Collapsible Sections
If you aren't taking advantage of various outlining features available in Visual Studio, you are missing out. In C# and HTML/ASP code, some blocks of code are automatically marked as collapsible by a + symbol in the far left margin of the code block's opening line. (It's just to the right of the line number if you have those enabled).
The C# code block with a plus sign at line 66, indicating a collapsible region for the method block.
The C# code after collapsing the section. Note the line jump from 66 to 79, and the … abbreviation.
#region Directive
In addition to the built-in collapsible sections you'll find in your code in Visual Studio, you can also manually add your own in C# (and even give them a "comment-style name") using the #region directive.
The C# code before adding a collapsible #region directive.
To add your own collapsible sections in C#:
- Place your cursor in the line above the section of code you want to make collapsible.
- Type #region
- Optionally, type additional text to act as a comment-style name on the same line. (This additional text will not be compiled, but will be displayed when you collapse the region).
- Place your cursor at the end of the section you are making collapsible.
- Type #endregion
The C# code after adding a collapsible #region directive. This one also includes an optional comment-style name of "Variables Declaration" on the same line as the opening #region.
You can now collapse the #region directive area like any other collapsible section:
- Click the + symbol in the left margin to collapse the region.
- OR click the - symbol in the left margin to expand it again to its full size.
The C# code after collapsing the #region directive. (Note that because we added a comment-style name after the region, that is what is displayed after the collapse.)
No comments:
Post a Comment