如何在C#中使用#if ..#elif ...#else ...#endif指令?
所有预处理程序指令均以#开头,并且一行上的预处理程序指令之前只能出现空格字符。预处理程序指令不是语句,因此它们不以分号(;)结尾。
#如果
#if指令允许测试一个或多个符号以查看它们是否为true。
#其他
它允许与#if一起创建复合条件指令。
#elif
它允许创建复合条件指令。
#万一
#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