Thursday, March 21, 2013

Using #Region in Dot Net

The #region directive allows organizing code files. Using #region directive we can separate the code. We can create separate region for each code block. Means to declare the variables, constants, functions etc we can use different code block by separating using #region directive.

Here is the example-

// region 1
#region Public Variables
Public int value1
Public int value2

//region 2
#region Methods
public string  GetData()

in above example we can see that I have created 2 region.

region1 for Public variables
region2 for methods

Like this we can create other regions also.

There are some benefits of using #region directive.

1. Separation of code
2. Readability
3. Easy to maintain
4. Easy to expand or collapse the code block
5. Easy to move entire code block

It is good to use #region directive while doing coding.


No comments:

Post a Comment