#116 – Use #region Directive to Create Code Regions

For improved readability, you can use the #region directive to create separate code regions in a C# source code file.  A code region can then be collapsed or expanded in the Visual Studio code editor.

In the example below, we surround a couple of functions with a single region called ThisAndThat.

 static void Main(string[] args)
 {
     DoThis();
     DoThat();
     uint x = 0x1234;
     x &= 0x0020;
 }

 #region ThisAndThat DoThis() and DoThat() functions that do stuff
 static void DoThis()
 {
     counter += 12;
 }

 static void DoThat()
 {
     counter *= 4;
 }
 #endregion

You’ll notice that in the Visual Studio editor, there is a little minus (-) sign to the left of the #region statement, indicating that this region is collapsible.

If you click on the minus (-) sign on the #region line, the entire region–both functions–collapse and you just see the name and comment associated with the region.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: