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
C++
In C++ you create regions by using “#pragma region” with label and “#pragma endregion” (case-specific):
C#
For C# you can eliminate the “pragma” keyword and just use “#region” with label and “#endregion” (case-specific):
VB
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
Anonymous
February 21, 2011
Zain, FYI, named C# regions do not require quotes. Thanks, Scott BlomfieldAnonymous
February 21, 2011
Using regions is like putting the toilet lid down without flushing.Anonymous
February 22, 2011
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? :)Anonymous
February 22, 2011
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 ;)Anonymous
February 22, 2011
The comment has been removedAnonymous
February 22, 2011
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!Anonymous
February 22, 2011
Large Code Regions = an excuse to not bother adhering to SRP :-)Anonymous
February 22, 2011
The comment has been removedAnonymous
February 23, 2011
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.Anonymous
April 01, 2012
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.