如何在 C# 中使用 #undef 指令?

csharpprogrammingserver side programming更新于 2025/7/13 0:22:17

#undef 指令允许您取消定义符号。语法如下:−

#undef SYMBOL

例如:

#undef One

与 #if 指令一起使用时,结果为 false。我们来看一个例子。 −

示例

#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

相关文章