C# - #region Preprocessor Directive Example

Learn about the #region preprocessor directive example in C# with the help of example.
Submitted by Nidhi, on October 31, 2020 [Last updated : March 23, 2023]

Here, we will demonstrate the example of #region preprocessor directive. We can create a region in the code using the #region preprocessor directive. The #region directive is used to organize the code, it cannot overlap a #if directive.

C# program to demonstrate the example of #region preprocessor directive

The source code to demonstrate the #region preprocessor directive is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# - #region Preprocessor Directive Example.
using System;

class Program
{
   public static void Main()
   {
        Console.WriteLine("Before Region");
#region RegionCountries
	Console.WriteLine("\tIndia");
	Console.WriteLine("\tAustralia");
	Console.WriteLine("\tAmerica");
	Console.WriteLine("\tEngland");
#endregion
        Console.WriteLine("After Region");
   }
}

Output

Before Region
        India
        Australia
        America
        England
After Region
Press any key to continue . . .

Explanation

In the above program, we created a class Program that contains the Main() method. Here, we created a region that contains some statements using the #region preprocessor directive.

Using the #region preprocessor directive, we can create a region in the code using the #region preprocessor directive. The #region directive is used to organize the code, it cannot overlap a #if directive.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.