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:

预处理器指令描述
#define它用于定义一系列成为符号的字符。
#undef它用于取消定义符号。
#if它用于测试符号是否为真。
#else它用于创建复合条件指令,与 #if 一起使用。
#elif它用于创建复合条件指令。
#endif指定一个条件指令的结束。
#line它可以让您修改编译器的行数以及(可选地)输出错误和警告的文件名。
#error它允许从代码的指定位置生成一个错误。
#warning它允许从代码的指定位置生成一级警告。
#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");
         #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 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");
      #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