Organize Your Code with Named Regions


Versions: 2005, 2008, 2010

Code: vstipEdit0095



Did you know you can create named Regions that can be used to organize your code? Let’s take a look at how to do it.


Creating Named Regions


In C++ you create regions by using “#pragma region” with label and “#pragma endregion” (case-specific):





For C# you can eliminate the “pragma” keyword and just use “#region” with label and “#endregion” (case-specific):





Visual Basic is just as easy as C# and uses a similar syntax of “#Region” with label and “#End Region” (not case-specific):





Value of Named Regions

The value of creating Regions is twofold. First, they travel with the code so are shared by all team members when using source control. Second, they become part of document outlining and can be collapsed or expanded to further organize you code:


Comments (10)

  1. C# Regions says:


    FYI, named C# regions do not require quotes.


    Scott Blomfield

  2. dkil1972 says:

    Using regions is like putting the toilet lid down without flushing.

  3. zainnab says:

    Scott — Yeah i struggled with that one for a bit but decided to show them with quotes for consistency with the VB ones

    dkil1972 — So…Regions are out for you? 🙂

  4. Casey Watson says:

    You'd better watch out there Zain… you're liable to ruffle a few feathers. In my experience, developers have strong feelings (for and against) code regions 😉

  5. zainnab says:

    Hehehe thanks Casey 🙂

    Thankfully I can claim to be a conscientious objector in this particular battle.  To paraphrase the Fox News tagline:  I report, you decide.


  6. Josh says:

    Yeah, I have to agree with dkil1972. Regions always seemed like toddler refactoring to me. I'll just push everything under this here… TADA! It's clean!

  7. Isaac says:

    Large Code Regions = an excuse to not bother adhering to SRP 🙂

  8. Yeah… there are some really strong opinions about regions in source code.  Richard Banks has started an "Anti-Region Campaign" on his blog and he lists some reasons why he doesn't like them:…/anti-region-campaign.html

  9. Chris Marisic says:

    I use resharper to autoblock all of my code into regions, region for using statements, region for members, fields, ctors, etc. This way you can collapse to outline and only need to expand the method or 2 you're actively developing. Most times things like using statements, fields, ctors and autoproperties are all just noise anyway.

  10. Clint says:

    Chris: How are you getting ReSharper to autoblock your code into regions?  I have ReSharper but haven't seen/found that.  I've been using nArrange to do it, but that tool is getting old and not working in all WPF code behind files.

Skip to main content