Hiding and Outlining Code: using the #region keyword in .NET

Quick tip: you can put sections of your code inside #region blocks in order to collapse and expand them using .NET Studio’s outlining feature. The text after the #region keyword is displayed next to the plus/minus sign, so keep it descriptive.

For example:


#region database code

public void DBFunc1() {
// .. a bunch of database code
}

public void DBFunc2() {
// .. a bunch of database code
}

#endregion

#region encryption code

public EncryptionFunc1() {
// .. a bunch of encryption code
}

public EncryptionFunc2() {
// .. a bunch of encryption code
}

#endregion

would give you two code regions named “database code” and “encryption code”. You could then collapse the regions you’re not interested so the code view is easier to manage.

You can also nest regions:


#region a big database function

public void BigDBFunction() {

#region some code
// a bunch of code...
#endregion

#region some more code
// a bunch of code...
#endregion

#region even more code
// a bunch of code...
#endregion

}
#endregion

to gain finer control of what code is shown or hidden. In the above example, you could hide the entire function, or just hide sections of it.

Check out MSDN’s page on outlining and hiding code to get more information on regions and what menu options are available. Happy outlining!

0