#116 – Use #region Directive to Create Code Regions
October 11, 2010 Leave a comment
For improved readability, you can use the #region directive to create separate code regions in a C# source code file. A code region can then be collapsed or expanded in the Visual Studio code editor.
In the example below, we surround a couple of functions with a single region called ThisAndThat.
static void Main(string[] args) { DoThis(); DoThat(); uint x = 0x1234; x &= 0x0020; } #region ThisAndThat DoThis() and DoThat() functions that do stuff static void DoThis() { counter += 12; } static void DoThat() { counter *= 4; } #endregion
You’ll notice that in the Visual Studio editor, there is a little minus (-) sign to the left of the #region statement, indicating that this region is collapsible.
If you click on the minus (-) sign on the #region line, the entire region–both functions–collapse and you just see the name and comment associated with the region.