Preprocessor Directives in C#

29 Mar, 2019
Preprocessor Directives in C# tell the compiler to process the given information before actual compilation of the program starts. It begins with a hashtag symbol (#) and since these preprocessors are not statements so no semi-colon is appended at the end. The C# compiler does not have a separate preprocessor, yet the directives are processed as if there was one. There cannot be anything else in a line other than the preprocessor directive.
The Preprocessors used in C# are given below:
Example 1: Using #define, #if, #else and #endif Let us understand this concept with a few examples. In the code given below, we are using #define to define a symbol called shape. So this means that ‘shape‘ evaluates to true. Inside main, we check if shape exists or not using #if. Since it does exists and the compiler knows it beforehand so the part #else will never be executed and is treated as a comment by the compiler. The #endif is used to indicate the end of the if derective.
// C# Program to show the use of// preprocessor directives  // Defining a symbol shape#define shape  using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;  namespace Preprocessor {  class Program {      static void Main(string[] args)    {        // Checking if symbol shape exists or not        #if (shape)           Console.WriteLine("Shape Exists");        #else           Console.WriteLine("Shape does not Exist");         // Ending the if directive       #endif    }}}
Output:
Shape Exists
Example 2 : Using #warning and #define Consider another example. In the code given below, we deliberately remove the definitions of symbols shape and shape_. Since the compiler cannot find these it executes the #else directive. Here, we generate user defined warning and error.
// C# program to show the Removal of// definition of shape and shape_  #undef shape_  using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;  namespace preprocessor2{    class Program    {        static void Main( string[] args )        {            // Checking if shape exists            #if (shape)                Console.WriteLine("Shape Exists");                              // Or if shape_ exists            #elif (shape_)                Console.WriteLine("Shape_ Exists" );                              #else              // using #warning to display message that            // none of the symbols were found            #warning "No Symbols found"              // Generating user defined error            #error "Check use of preprocessors"              // Ending if            #endif          }    }}
This code will not compile as there exists an error in the code. Warnings and errors essentially do the same job but the error will stop the compilation process of the code. The visual studio will give you the following result:
Example 3: Using #region and #endregion #region defines a set of instructions as a block of code and this block is compiled all at once by the compiler. #endregion marks the end of the block. The program below depicts the same.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;  namespace preprocessor3{    class Program    {        static void Main( string[] args )        {            char ch = 'y';              // Using #region to define a block of code            #region            if (ch == 'y' || ch == 'Y')                Console.WriteLine( "Value of ch is 'y'" );            else                Console.WriteLine( "Value of ch is unknown" );              // Ends the region            #endregion        }    }}
Value of ch is 'y'

Example 4: Using #pragma warning and #pragma checksum In the code below we use the #pragma warning disable to disable all warnings. Inside main, we generate a user defined warning to check if it has been disabled or not. And #pragma checksum is used to aid the debugging of the file.
// C# program to Disables all warnings  #pragma warning disable  using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;  namespace Preprocessor4 {      class GFG {          static void Main(string[] args)    {        // Creating a warning        #warning "This is disabled"                  // Checksum is used for debugging         // the file in consideration        #pragma checksum "Program.cs"    }}}
The error list for the above code:
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.