English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Preprocessor Directives

Preprocessor instructions guide the compiler to preprocess information before the actual compilation begins.

All preprocessor instructions start with #. And on a line, only whitespace characters can appear before the preprocessor instruction. Preprocessor instructions are not statements, so they do not end with a semicolon (;).

The C# compiler does not have a separate preprocessor, but the instructions are processed as if there were a separate preprocessor. In C#, preprocessor instructions are used to take effect in conditional compilation. Like C and C++ The difference is that they are not used to create macros. A preprocessor instruction must be the only instruction on the line.

C# Preprocessor Instruction List

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

Preprocessor instructionsDescription
#defineIt is used to define a series of characters that become symbols.
#undefIt is used to undefine a symbol.
#ifIt is used to test if a symbol is true.
#elseIt is used to create compound conditional instructions, used together with #if.
#elifIt is used to create compound conditional instructions.
#endifSpecifies the end of a conditional instruction.
#lineIt allows you to modify the line number of the compiler and (optionally) the filename of the output error and warning messages.
#errorIt allows you to generate an error from a specified location in the code.
#warningIt allows you to generate a level 1 warning from a specified location in the code.
#regionIt allows you to specify an expandable or collapsible code block when using the Visual Studio Code Editor's outline feature.
#endregionIt indicates the end of the #region block.

#define preprocessor

#define preprocessor instructions create symbol constants.

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

#define symbol

The following program illustrates this point:

#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 above code is compiled and executed, it will produce the following result:

PI is defined

Conditional directives

You can use the #if directive to create a conditional directive. Conditional directives are used to test if a symbol is true. If it is true, the compiler will execute the code between #if and the next directive.

Syntax of conditional directives:

#if symbol [operator symbol]...

Among them,symbol is the name of the symbol to be tested. You can also use true and false, or place a negation operator before the symbol.

Common operators include:

  • == (equal)

  • != (not equal)

  • && (and)

  • || (or)

You can also group symbols and operators with parentheses. Conditional directives are used to compile code when debugging or compiling a specified configuration. A symbol that #if The conditional directive at the beginning of the directive must be explicitly marked with a #endif Directive terminated.

The following program demonstrates the usage of conditional directives:

#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_V"}10 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 above code is compiled and executed, it will produce the following result:

DEBUG and VC_V10 are defined