10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

Preprocessor Directives in C#

Updated on Sep 3, 2025
 
46,006 Views

Preprocessor directives are a block of statements that are handled before the actual compilation process starts. They give some instructions to the compiler on the compilation process, error or warning handling etc.

Preprocessor directives begin with a hash symbol (#) and do not contain the semicolon at the end as they are not statements. They are instead terminated by a new line.

Some of the preprocessor directives in C# are given as follows:

Preprocessor Directive

Description

#if

The #if directive compiles the code between the directives only if the specified symbol is defined.

#else

The #else directive allows the user to create a compound conditional directive. If none of the expressions in the preceding #if or the #elif directives evaluate to true, then the compiler evaluates all the code between #else and the subsequent #endif.

#elif

The #elif is a compound conditional directive that is evaluated if neither the preceding #if nor any preceding optional #elif directive expressions evaluate to true.

#endif

The #endif directive specifies the end of a conditional directive which began with the #if directive.

#define

The #define directive is used to define a symbol, which is a sequence of characters.

#undef

The #undef directive lets the user to undefine a symbol.

#warning

The #warning directive is used to generate a CS1030 level one compiler warning from a specific location in the code.

#error

The #error directive allows the user to generate a CS1029 user-defined error from a specific location in your code.

#line

The #line directive lets the user modify the compiler's line numbering and also optionally the file name output for errors and warnings

#region

The #region directive allows the user to specify a block of code that be expanded or collapsed when using the outlining feature of the Visual Studio Code Editor.

#endregion

The #endregion directive marks the end of a #region block.

#pragma

The #pragma directive provides the compiler special instructions for the compilation of the file in which it appears.

#pragma warning

The #pragma warning directive can enable or disable certain warnings.

#pragma checksum

The #pragma checksum directive generates checksums for source files to help with debugging ASP.NET pages.

Table: Preprocessor directives in C#

We will explore examples for various preprocessor directives.

The #define Preprocessor Directive

The #define directive is used to define a symbol, which is a sequence of characters. The symbols that are defined using the #define directive evaluate to true when used with the #if directive.

A program that demonstrates the #define directive is given as follows:

#define TEST
using System;
namespace PreprocessorDirectivesDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        #if (TEST)
           Console.WriteLine("The TEST is defined");
        #else
           Console.WriteLine("The TEST is not defined");
        #endif
     }
  }
}

Source Code: Program that demonstrates the #define directive in C#

The output of the above program is as follows:

The TEST is defined

The #undef Preprocessor Directive

The #undef directive allows the user to undefine a symbol. The symbols that are undefined using the #undef directive evaluate to false when used with the #if directive.

A program that demonstrates the #undef directive is given as follows:

#undef TEST
using System;
namespace PreprocessorDirectivesDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        #if (TEST)
           Console.WriteLine("The TEST is defined");
        #else
           Console.WriteLine("The TEST is not defined");
        #endif
     }
  }
}

Source Code: Program that demonstrates the #undef directive in C#

The output of the above program is as follows:

The TEST is not defined

The #if Preprocessor Directive

The #if directive compiles the code between the directives only if the specified symbol is defined. It is basically used to create a conditional directive. This means that the code that is inside the #if directive is only compiled if the expression tested with the #if directive evaluates to true. The #endif directive always the if directive.

A program that demonstrates the #if directive is given as follows:

#define TEST
using System;
namespace PreprocessorDirectivesDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        #if (TEST)
           Console.WriteLine("The TEST is defined");
        #endif
     }
  }
}

Source Code: Program that demonstrates the #if directive in C#

The output of the above program is as follows:

The TEST is defined

The #elif Preprocessor Directive

The #elif is a compound conditional directive that is evaluated if neither the preceding #if nor any preceding optional #elif directive expressions evaluate to true. This preprocessor directive is used when multiple expressions are tested.

A program that demonstrates the #elif directive is given as follows:

#undef TEST1
#define TEST2
using System;
namespace PreprocessorDirectivesDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        #if (TEST1 && TEST2)
Console.WriteLine("Both TEST1 and TEST2 are defined");
#elif (TEST1 && !TEST2)
Console.WriteLine("TEST1 is defined and TEST2 is undefined");
#elif (!TEST1 && TEST2)
Console.WriteLine("TEST1 is undefined and TEST2 is defined");
#else
Console.WriteLine("Both TEST1 and TEST2 are undefined");
#endif
     }
  }
}

Source Code: Program that demonstrates the #elif directive in C#

The output of the above program is as follows:

TEST1 is undefined and TEST2 is defined

The #warning Preprocessor Directive

The #warning directive is used to generate a CS1030 level one compiler warning from a specific location in the code.

A program that demonstrates the #warning directive is given as follows:

using System;
namespace PreprocessorDirectivesDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        #if (!TEST)
#warning TEST is undefined
#endif
     }
  }
}

Source Code: Program that demonstrates the #warning directive in C#

The output of the above program is as follows:

main.cs(11,0): warning CS1030: #warning: `TEST is undefined'
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses