C# preprocessor

In this chapter you will learn:

  1. A list of preprocessor commands available in C#
  2. All C# Preprosessor directives
  3. Example for Preprocessor directives

Description

Preprosessor directives mark the code block for compiler.

For example,


#define DEBUG/*from   w  w  w  . jav  a2  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   www.  jav a  2  s.com
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  ww  w  . j a v  a 2 s.  c  om
#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.

Next chapter...

What you will learn in the next chapter:

  1. What is Preprocessing Directives #define
  2. How to use Preprocessing Directives #define
  3. Example for Preprocessing Directives #define
  4. Use the Conditional attribute with #define
Home »
  C# Tutorial »
    C# Types »
      C# Preprocessor
C# preprocessor
C# #define Preprocessor
C# #if Preprocessing Directive
C# #elif Preprocessing Directive
C# #undef
C# #line directive
C# #region directive
C# #warning preprocessor