[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Understand region directives in C#

[Understand region directives in C#]

Region directives let you define sections of code that you can collapse and expand by clicking the - and + signs to the left in the code window. Each region directive should have a corresponding endregion directive.

You can include optional text at the end of region directives to indicated which region is beginning or ending. If you include that text, then Visual Studio displays it when the region is collapsed.

Note, however, that Visual Studio doesn't really do anything with after an endregion directive. Any text there is purely decorative and for your use only.

For example, suppose your program contains a long series of graphics methods. The following code shows how you might use region directives to make it easy to hide and show the graphics methods as a group.

... #region Graphics Methods private void DrawCircle(...) { ... } private void DrawRectangle(...) { ... } private void DrawHypotrochoid(...) { ... } #endregion Graphics Methods

Now when you're not working on the graphics methods, you can hide their region so they don't clutter up the editor. (I often have code with all or most of the methods contained in a handful of regions so I only need to have the one I'm working on open at a given moment.)

You've probably seen region directives before. Even if you have, however, here are two region-related puzzles for you.

  1. What happens if your code includes regions that are not properly nested as in the following example?

private void Form1_Load(object sender, EventArgs e) { int a = 1; #region "Region1" a++; #region "Region2" a++; #endregion "Region1" a++; #endregion "Region2" a++; }

  1. What happens if a region lies partly inside and partly outside of a method as in the following example?

#region Region3 private void Test1() { int b = 0; b++; } private void Test2() { int b = 0; #endregion Region3 b++; }

You can easily try these out to see what happens so I won't make this a puzzle to be solved in a later post. If you do want to think about it or try it out, do so now before reading the solutions that follow.


Okay, here are the answers.

  1. What happens if your code includes regions that are not properly nested as in the following example?

Visual Studio completely ignores the text after the region and endregion directives. It simply matches each endregion directive with the closest preceding region directive. That means the previous code is equivalent to the following.

private void Form1_Load(object sender, EventArgs e) { int a = 1; #region Region1 a++; #region Region2 a++; #endregion Region2 a++; #endregion Region1 a++; }

Even though Visual Studio ignores the strings after these directives, I recommend that you use them to make the code more readable.

  1. What happens if a region lies partly inside and partly outside of a method as in the following example?

I think this one is even more interesting. When you collapse a region that includes only part of a method, the region collapses the code up to the end of the method. This way you're not left staring in confusion at half of a method.

The same is not true if the region starts inside a method and includes only part of a for loop or other block inside the method.

Finally, Visual Studio ignores a region if you start it inside a method and don't end it in the same method. (I'm actually surprised it doesn't just get confused and refuse to compile.)

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.