Csharp 简明教程

C

预处理器指令指示编译器在开始实际编译之前预处理信息。

The preprocessor directives give instruction to the compiler to preprocess the information before actual compilation starts.

所有预处理器指令都以 # 开头,且每行只能在预处理器指令前出现空白字符。预处理器指令不是语句,因此它们不以分号 (;) 结束。

All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not statements, so they do not end with a semicolon (;).

C# 编译器没有单独的预处理器;但是,对指令的处理就像有预处理器一样。在 C# 中,预处理器指令用于帮助条件编译。与 C 和 C++ 指令不同,它们不用于创建宏。预处理器指令必须是行上的唯一指令。

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

Preprocessor Directives in C

下表列出了 C# 中可用的预处理器指令 −

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

Sr.No.

Preprocessor Directive & Description

1

#define It defines a sequence of characters, called symbol.

2

#undef It allows you to undefine a symbol.

3

#if It allows testing a symbol or symbols to see if they evaluate to true.

4

#else It allows to create a compound conditional directive, along with #if.

5

#elif It allows creating a compound conditional directive.

6

#endif Specifies the end of a conditional directive.

7

#line It lets you modify the compiler’s line number and (optionally) the file name output for errors and warnings.

8

#error It allows generating an error from a specific location in your code.

9

#warning It allows generating a level one warning from a specific location in your code.

10

#region It lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor.

11

#endregion It marks the end of a #region block.

The

#define 预处理器指令创建符号常量。

The #define preprocessor directive creates symbolic constants.

#define 允许定义一个符号,这样,通过使用符号作为传递给 #if 指令的表达式,该表达式将计算为 true。其语法如下 -

#define lets you define a symbol such that, by using the symbol as the expression passed to the #if directive, the expression evaluates to 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 result −

PI is defined

Conditional Directives

您可以使用 #if 指令创建条件指令。条件指令对于检测一个符号或多个符号以检查它们是否计算为 true 非常有用。如果它们确实计算为 true,编译器将计算 #if 和下一个指令之间的所有代码。

You can use the #if directive to create a conditional directive. Conditional directives are useful for testing a symbol or symbols to check if they evaluate to true. If they do evaluate to true, the compiler evaluates all the code between the #if and the next directive.

条件指令的语法如下 -

Syntax for conditional directive is −

#if symbol [operator symbol]...

其中,符号是你想要检测的符号的名称。您还可以使用真和假,或用否定运算符给符号添加前缀。

Where, symbol is the name of the symbol you want to test. You can also use true and false or prepend the symbol with the negation operator.

运算符符号是用于计算符号的运算符。运算符可以是下列任一者 -

The operator symbol is the operator used for evaluating the symbol. Operators could be either of the following −

  1. == (equality)

  2. != (inequality)

  3. && (and)

  4. || (or)

您还可以用括号对符号和运算符进行分组。条件指令用于编译调试版本或特定配置的代码。以 #if 指令开头的条件指令必须以 #endif 指令明确终止。

You can also group symbols and operators with parentheses. Conditional directives are used for compiling code for a debug build or when compiling for a specific configuration. A conditional directive beginning with a #if directive must explicitly be terminated with a #endif directive.

以下程序演示如何使用条件指令 -

The following program demonstrates 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 result −

DEBUG and VC_V10 are defined