Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# Preprocessor Directives

C# Preprocessor Directives

Preprocessor Directives instruct the compiler to process information before actual compilation begins.

Through these directives, you can control how the compiler compiles a file or which parts it compiles. Common preprocessor directives include conditional compilation, macro definitions, and so on.

All preprocessor directives begin with #, and on a line, only whitespace characters may appear before a preprocessor directive.

Preprocessor directives are not statements, so they do not end with a semicolon ;.

The C# compiler does not have a separate preprocessor, but directives are processed as if there were one. In C#, preprocessor directives are used in conditional compilation. Unlike C and C++, they are not used to create macros. A preprocessor directive must be the only directive on a line.

List of C# Preprocessor Directives

The following table lists the preprocessor directives available in C#:

Instruction Description
#define Defines a symbol that can be used in conditional compilation.
#undef Undefines a symbol.
#if Begins a conditional compilation block; includes the code block if the symbol is defined.
#elif If the preceding #if or #elif condition is not met and the current condition is met, includes the code block.
#else If the preceding #if or #elif includes the code block if the condition is not met.
#endif Ends a conditional compilation block.
#warning Generates a compiler warning message.
#error Generates a compiler error message.
#region Marks a code region that can be collapsed and expanded in the IDE, making the code easier to organize and read.
#endregion Ends a code region.
#line Changes the line number and file name in compiler output; can be used for debugging or generated tool code.
#pragma Sends special instructions to the compiler, such as disabling or restoring specific warnings.
#nullable Controls the nullability context and annotations, allowing compiler checks for nullable reference types to be enabled or disabled.

Example

#define DEBUG

#if DEBUG

    Console

.

WriteLine

(

"Debug mode"

)

;

#elif RELEASE

    Console

.

WriteLine

(

"Release mode"

)

;

#else

    Console

.

WriteLine

(

"Other mode"

)

;

#endif

#warning This is a warning message

#error This is an error message

#region MyRegion

   

// Your code here

#endregion

#line 100 "MyFile.cs"

   

// The next line will be reported as line 100 in MyFile.cs

    Console

.

WriteLine

(

"This is line 100"

)

;

#line default

   

// Line numbering returns to normal

#pragma warning disable 414

   

private

int

unusedVariable

;

#pragma warning restore 414

#nullable enable

   

string

?

nullableString

=

null

;

#nullable disable

#define and #undef Preprocessors

#define is used to define a symbol (usually for conditional compilation), and #undef is used to undefine a symbol.

#define DEBUG



#undef RELEASE

#define allows you to define a symbol so that by using the symbol as the expression passed to the #if directive, the expression returns true. Its syntax is as follows:



#define symbol

The following program illustrates this:

Example

#define PI

using

System

;

namespace

PreprocessorDAppl

{

   

class

Program

   

{

     

static

void

Main

(

string

[

]

args

)

     

{

         

#if (PI)

            Console

.

WriteLine

(

"PI is defined"

)

;

         

#else

            Console

.

WriteLine

(

"PI is not defined"

)

;

         

#endif

         Console

.

ReadKey

(

)

;

     

}

   

}

}

When the code above is compiled and executed, it produces the following result:



PI is defined

Conditional Directives: #if, #elif, #else, and #endif

You can use the #if directive to create a conditional directive.

Conditional directives are used to test whether a symbol is true. If it is true, the compiler executes the code between #if and the next directive.

Syntax of a conditional directive:



#if symbol [operator symbol]...

Wheresymbol is the name of the symbol to test. You can also use true and false, or place a negation operator before the symbol.

Common operators include:

You can also group symbols and operators with parentheses. Conditional directives are used to compile code in debug builds or when compiling a specified configuration. A conditional directive that begins with a #if directive must explicitly terminate with an #endif directive.

#define DEBUG



#if DEBUG

    Console.WriteLine("Debug mode");

#elif RELEASE

    Console.WriteLine("Release mode");

#else

    Console.WriteLine("Other mode");

#endif

The following program demonstrates the use of conditional directives:

Example

#define DEBUG

#define VC_V10

using

System

;

public

class

TestClass

{

   

public

static

void

Main

(

)

   

{

     

#if (DEBUG && !VC_V10)

         Console

.

WriteLine

(

"DEBUG is defined"

)

;

     

#elif (!DEBUG && VC_V10)

         Console

.

WriteLine

(

"VC_V10 is defined"

)

;

     

#elif (DEBUG && VC_V10)

         Console

.

WriteLine

(

"DEBUG and VC_V10 are defined"

)

;

     

#else

         Console

.

WriteLine

(

"DEBUG and VC_V10 are not defined"

)

;

     

#endif

      Console

.

ReadKey

(

)

;

   

}

}

When the code above is compiled and executed, it produces the following result:



DEBUG and VC_V10 are defined

#warning and #error

#warning is used to generate a compiler warning, and #error is used to generate a compiler error.

#warning This is a warning message

#error This is an error message

#region and #endregion

Used for code folding, making code more readable.

#region MyRegion

    // Your code here

#endregion

#line

Used to change the compiler output of the file line number and file name.

#line 100 "MyFile.cs"

    // The next line will be reported as line 100 in MyFile.cs

    Console.WriteLine("This is line 100");

#line default

    // Line numbering returns to normal

#pragma

Used to send special instructions to the compiler. The most common use is to disable specific warnings.



#pragma warning disable 414

    private int unusedVariable;

#pragma warning restore 414

Notes on Using Preprocessor Directives

By using these preprocessor directives correctly, you can better control the compilation process and improve code flexibility and maintainability.