所有预处理程序指令均以#开头,并且一行上的预处理程序指令之前只能出现空格字符。预处理程序指令不是语句,因此它们不以分号(;)结尾。
#if指令允许测试一个或多个符号以查看它们是否为true。
它允许与#if一起创建复合条件指令。
它允许创建复合条件指令。
#endif指定条件指令的结尾。
以下是显示#if,#elif,#else和#endif指令用法的示例-
#define One
#undef Two
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
#if (One && TWO)
Console.WriteLine("Both are defined");
#elif (ONE && !TWO)
Console.WriteLine("ONE is defined and TWO is undefined");
#elif (!ONE && TWO)
Console.WriteLine("ONE is defined and TWO is undefined");
#else
Console.WriteLine("Both are undefined");
#endif
}
}
}输出结果
Both are undefined