C# Tutoriallogin
C# Tutorial
author:php.cn  update time:2022-04-11 14:06:23

C# preprocessor directives



# Preprocessor directives instruct the compiler to preprocess information before actual compilation begins.

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

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

C# List of preprocessor directives

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

Preprocessor DirectiveDescription
#defineIt is used to define a series of characters that become symbols.
#undefIt is used to undefine a symbol.
#ifIt is used to test whether the 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.
#line It lets you modify the number of lines the compiler uses and (optionally) the filenames where errors and warnings are output.
#errorIt allows generating an error from a specified location in the code.
#warningIt allows generating a first-level warning from a specified location in the code.
#regionIt allows you to specify an expandable or collapsed code block when using the Outline feature of Visual Studio Code Editor.
#endregionIt marks the end of the #region block.

#define Preprocessor

#define preprocessor directive creates symbolic constants.

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

#define symbol

The following program illustrates this:

#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 produces the following results:

PI is defined

Conditional Directives

You can use the #if directive to create a conditional directive. Conditional instructions are used to test whether a symbol is true. If true, the compiler executes the code between #if and the next instruction.

The syntax of conditional instructions:

#if symbol [operator symbol]...

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

Operator symbols are operators used to evaluate symbols. The can operator can be one of the following operators:

  • == (equality)

  • != (inequality)

  • && (and)

  • ##|| (or)

You can also use parentheses to enclose symbols and operators Make groups. Conditional directives are used to compile code in a debug build or when compiling a specified configuration. A conditional directive that begins with a

#if directive must explicitly end with an #endif directive.

The following program demonstrates the use 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_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 above code is compiled and executed, it produces the following results:

DEBUG and VC_V10 are defined