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


#undef 指令允許您取消定義一個符號。以下為語法 −

#undef SYMBOL

例如,

#undef One

與 #if 指令結合使用時,它會求值為假。讓我們看一個示例 −

示例

 即時演示

#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

更新時間: 20-6 月-2020

223 個瀏覽

開啟你的 職業生涯

完成本課程以獲得證書

開始
廣告
© . All rights reserved.