Latest web development tutorials

C # preprocessor directives

Instruct the compiler preprocessor before the actual start compiling the information pretreatment.

All preprocessor directives are starting with #. And on one line, only blank characters can appear before a preprocessor directive. Preprocessor directive is not a statement, so they are not a semicolon (;) end.

C # compiler does not have a separate pre-processor, however, like to have a separate preprocessor as instruction is processed. In C #, the preprocessor directives for conditional compilation work. C and C ++ do not have different instructions, they are not used to create a macro. A preprocessor directive must be the only instruction on that line.

C # preprocessor directives list

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

#else它用于创建复合条件指令,与 #if 一起使用。
#region它可以让您在使用 Visual Studio Code Editor 的大纲特性时,指定一个可展开或折叠的代码块。
#endregion它标识着 #region 块的结束。

#define preprocessor

#define preprocessor directive creates symbolic constants.

#define allows you to define a symbol, so that, through the use of the symbol is passed to the #if directive expression as the expression returns 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");
            Console.WriteLine ( "PI is not defined");
         Console.ReadKey ();

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

PI is defined

Conditional instructions

You can use the #if directive to create a conditional instruction. Conditional instructions for testing whether the symbol is true. If true, the compiler will execute the code between #if and the next instruction.

Conditional instruction syntax:

#if symbol [operator symbol] ...

Wherein,symbol is the symbolic name to be tested. You can also use true and false, or symbol is placed in front of the negation operator.

Operator symbolsare used to evaluate the operator symbol. Operators can be one of the following operators:

  • == (Equality)
  • ! = (Inequality)
  • && (And)
  • || (Or)

You can also use parentheses to group symbols and operators. Conditional instruction in the debugging version or compile the specified configuration compiled code. A conditional instruction starting withthe #if directive, must be displayed in a #endifinstruction is terminated.

The following program demonstrates the use of conditional instructions:

#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");
         Console.WriteLine ( "DEBUG and VC_V10 are not defined");
      Console.ReadKey ();

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

DEBUG and VC_V10 are defined