Open In App

Preprocessor Directives in C#

Last Updated : 29 Mar, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Preprocessor Description
#define To define a Symbol
#undef Removes any definition of a symbol
#if Checks if the symbol evaluates to true
#endif Ends the conditional directive which began with #if
#else If the symbolic value of #if evaluates to false the #else directive statements are executed
#elif Creates a compound conditional directive which is executed if the symbolic value is true
#error Creates a user defined error
#warning Creates a user defined warning
#line Modifies the compiler’s default line numbering
#region Specifies a block of code that can be expanded or collapsed
#endregion Specifies the end of a region
#pragma Gives the compiler information for compilation of the file
#pragma warning Used for enabling or disabling warnings
#pragma checksum Creates checksums for source files

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
        }
    }
}


Output:

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:



Like Article
Suggest improvement
Share your thoughts in the comments