Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

Using Regions to Improve Code Readability
By James Divine

Source Code MyClass.cs

Platform Used
Visual Studio .NET

Article Description

An instructor I once had made a statement that has echoed in my head for years. "When you are a programmer, you have two groups of 'end users', the people who will actually use the software, AND other programmers who may someday have to modify your code." With that in mind, I have always tried to write my code to be as easily readable to other programmers as possible. Visual Studio .NET gives us some very handy tools to improve readability in our code. One that I am particularly fond of is the #region directive that allows us to collapse code in a customized manor.

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 create regions for each different type of elements in a class (i.e. constructors, variables, methods, properties, enumerators) so that my code can be easily navigated.

Here is an example:

Doing this will keep your code more organized, which in turn will increase efficiency. And someday down the road when another programmer comes across your source code, they will be very thankful.

About the Author: James Divine (james@4divine.com) is a Senior Software Developer with over 12 years experience working with web programming, desktop application programming, server application programming and handheld device programming. He has been working in .Net and C# for more than two years. James currently holds a Microsoft Certified Professional certification and is working towards the MCSD.