Understand region directives in C#

[region directives]

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 Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in coding, syntax and tagged , , , , , , , , , , . Bookmark the permalink.

3 Responses to Understand region directives in C#

  1. Mike Griffiths says:

    Having a fuzzy day Rod?
    Of course the compiler ignores regions that overlap method ends – the compiler ignores all region tags always – as they are only of interest to the presentation layer of the IDE.
    Best Regards.

  2. Rod Stephens says:

    Yeah, that’s kind of the point of the post.

    I am a bit surprised, though. It’s not a very Microsoftish approach.

  3. Rod Stephens says:

    Actually upon further reflection, I think yours is a separate point and worth emphasizing. The post is about the behavior of Visual Studio’s code editor. Yours is about the compiler. It is important to realize that the region directive is for use by the code editor and not the compiler.

    It still seems odd to me that the code editor doesn’t care about matching statements. It also seems a bit odd that this is handled by a directive when all of the other directives (AFAIK) are instructions for the compiler.

    In any case, there’s no harm done.

Leave a Reply

Your email address will not be published. Required fields are marked *