What is C# preprocessor

Description

Preprosessor directives mark the code block for compiler.

For example,


#define DEBUG/*ww  w .ja  v a  2 s  . co m*/
using System;
class MyClass
{
    int x;
    void aMethod()
    {
# if DEBUG
        Console.WriteLine("Testing: x = {0}", x);
# endif
    }

}

If DEBUG is defined C# will compile the statement.

We can supply the flag with /define:Symbol through commandline.

The code above generates the following result.

C# Preprosessor directives

The following table lists all C# Preprosessor directives

Preprocessor directiveAction
#define symbolDefines symbol
#undef symbolUndefines symbol
#if symbol [operator symbol2]...operators are ==, !=, &&, and || followed by #else, #elif, and #endif
#elseExecutes code to subsequent #endif
#elif symbol [operator symbol2]Combines #else branch and #if test
#endifEnds conditional directives
#warning textwarning text for compiler output
#error texterror text for compiler output
#line [ number ["file"] | hidden]number specifies the line in source code; file is the filename to appear in computer output; hidden instructs debuggers to skip over code from this point until the next #line directive
#region namebeginning of an outline
#end regionEnds an outline

C# uses the preprocessor directives to control the compilation. The preprocessor directives begin with # and it must appear on its own line. The preprocessor directives conditional compilation is #if, #else and, #endif, and #elif.

The #if directive tells the compiler to ignore a section of code unless a specified symbol has been defined. To define a symbol we use the #define or set it in the compilation. #define directives must be at top of file. Symbol names are uppercase by convention.


#define TESTMODE/*from ww w  .  j  a va2 s . c om*/
using System;

class Program
{
    public static void Main()
    {
#if TESTMODE
        Console.WriteLine("in test mode!"); // OUTPUT: in test mode!
#endif
    }
}

The output:

To define a symbol assembly-wide, specify the /define switch when compiling:

csc Program.cs /define:TESTMODE,ANOTHER

Preprocessor directivesC# statements
#ifif
#elseelse
#elif or #else #ifelse if

The ||, &&, and ! operators can be used to perform or, and, and not operations.

Logical Operators Supported by the #if..#endif Directive

OperatorExampleDescription
==#if winXP == trueEvaluates to true if the symbol winXP is defined. Equivalent to #if winXP.
!=#if winXP != trueEvaluates to true if the symbol winXP is not defined. Equivalent to #if !winXP.
&& #if winXP && releaseEvaluates to true only if the symbols winXP and release are defined.
||#if winXP || releaseEvaluates to true if either of the symbols winXP or release are defined.
()#if (winXP || win7) && releaseParentheses allow you to group expressions. Evaluates to true if the symbols winXP or win7 are defined and the symbol release is defined.

Example

The following code uses #define, #undef, #elif and #else to do the conditional compilation.


#define win7//from   w  w w .j  av  a  2 s  .  co  m
#define release
#undef  win2000

using System;
using System.Diagnostics;

class MainClass
{
    [Conditional("DEBUG")]
    public static void DumpState()
    {
        Console.WriteLine("Dump some state...");
    }

    public static void Main()
    {
        string platformName;
#if winXP          // Compiling for Windows XP 
                   platformName = "Microsoft Windows XP"; 
#elif win2000      // Compiling for Windows 2000 
                   platformName = "Microsoft Windows 2000"; 
#elif win7        // Compiling for Windows 7
        platformName = "Microsoft Windows 7";
#else              // Unknown platform specified 
                   platformName = "Unknown"; 
#endif

        Console.WriteLine(platformName);
    }
}

To build the example and define the symbols winXP, use the command csc /define:winXP; Example.cs.

The code above generates the following result.





















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor