Where to use #region directive in C#?


It lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. It should be terminated with #endregion.

Let us see how to define a region using #region.

#region NewClass definition
public class NewClass {
   static void Main() { }
}
#endregion

The following is an example showing the usage of #region directive.

Example

 Live Demo

using System;
#region
class MyClass {
}

#endregion
class Demo {
   #region VARIABLE
   int a;
   #endregion
   static void Main() {
      #region BODY
      Console.WriteLine("Example showing the usage of region directive!");
      #endregion
   }
}

Output

Example showing the usage of region directive!

Updated on: 22-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements