Using Regions in C# to Improve Code Readability
An instructor I once had made a statement thathas echoed in my head for years. "When you are a programmer, you havetwo groups of 'end users', the people who will actually use thesoftware, AND other programmers who may someday have to modify yourcode." With that in mind, I have always tried to write my code to be aseasily readable to other programmers as possible.Visual Studio .NET gives us some very handy tools to improvereadability in our code. One that I am particularly fond of is the#region directive that allows us to collapse code in a customizedmanor.
Regions are created in this format:
#region MyRegion
your code here
#endregion
Everything that falls within the directives will be collapsible.
The method I've chosen to adapt is to createregions for each different type of elements in a class (i.e.constructors, variables, methods, properties, enumerators) so that mycode can be easily navigated.
Here is an example:

Doing this will keep your code moreorganized, which in turn will increase efficiency. And someday down theroad when another programmer comes across your source code, they willbe very thankful.
About the Author: James Divine(james@4divine.com) is a Senior Software Developer with over 12 yearsexperience working with web programming, desktop applicationprogramming, server application programming and handheld deviceprogramming. He has been working in .Net and C# for more than twoyears. James currently holds a Microsoft Certified Professionalcertification and is working towards the MCSD.












No comments yet... Be the first to leave a reply!