Visual C# Express - Code Folding
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| Visual C# Express: Code Folding |
This is one of those nice features which makes you appreciate having an IDE instead of using notepad. Code folding allows you to minimise chunks of code into a single line, so that the Code Editor window does not become too cluttered or overly long.
The IDE automatically groups the code at various boundaries (method, property, class, structure, interface, and namespace bodies). It then allows you to expand/collapse the regions of these code body groups using the familiar plus/minus sign used in the tree-view control (e.g., in Windows Explorer) which allows you to fold (collapse)/unfold (expand) the code regions.
You can also define your own regions to be folded using #region and #endregion keywords. These can encapsulate as many methods or program statements as you wish. When you define a region, any text that appears to the right of the #region keyword is considered to be the name of the region. This may be more that a single word and can be used to write a short description of the code inside the folded region.
An example
When you create a console application using C# Express, the default code is a simple empty application. The code contains a single method that looks like the following code snippet. On the left hand side of the code is a minus symbol that can be clicked on to reduce the code, indicated by the line below it, into a single line.
Figure 7: Visual C# Express - Unfolded Code
When the code is folded, it now looks like the following code snippet. Clicking on the plus symbol will unfold the code back to its previous state.
Figure 8: Visual C# Express - Folded Code
In this manner, code can be reduced to a set of high level statements with all of the intricate code hidden within the folded code. Often this can reduce the amount of information that you are reading into a more manageable block.
Figure 9: Visual C# Express - Code Folding
Summary
- Code folding can reduce the amount of information that you are reading into more manageable chunks of information.
- Code folding allows you to minimise chunks of code into a single line.
- Code is grouped at various boundaries (method, property, class, structure, interface, and namespace bodies).
- These groups can be expanded or collapsed by clicking on the plus and minus symbols.
- You can define your own regions to be folded using
#regionand#endregionkeywords.
- Your own regions can encapsulate as many methods or program statements as you wish.
- The code inside a folded method is represented by three periods (dots) in a box.
- The name of the region is displayed inside a box to define where the folded code is.
- When displaying line numbers, they are removed for the folded code.
|

