C# Language Comments and regions Regions

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

A region is a collapsible block of code, that can help with the readability and organisation of your code.

NOTE: StyleCop's rule SA1124 DoNotUseRegions discourages use of regions. They are usually a sign of badly organized code, as C# includes partial classes and other features which make regions obsolete.

You can use regions in the following way:

class Program
{
    #region Application entry point
    static void Main(string[] args)
    {
        PrintHelloWorld();
        System.Console.ReadLine();
    }
    #endregion

    #region My method
    private static void PrintHelloWorld()
    {
        System.Console.WriteLine("Hello, World!");
    }
    #endregion
}

When the above code is view in an IDE, you will be able to collapse and expand the code using the + and - symbols.

Expanded

The above code in Visual Studio

Collapsed

The above code in Visual Studio Collapsed using regions



Got any C# Language Question?