處理 # 開頭指令:巨集定義、條件編譯、錯誤警告輸出、程式碼區塊摺疊,屬編譯前文字替換階段。
#define DEBUG
#undef DEBUG
定義/移除符號(無數值,僅存在與否)。順序決定最終狀態:先 #define 後 #undef → 未定義。
#if DEBUG
Console.Write("debug");
#elif RELEASE
Console.Write("release");
#else
Console.Write("other");
#endif
依符號定義決定編譯哪段代碼。同時定義 DEBUG 與 RELEASE 時,優先符合 #if DEBUG。
#if DEBUG
#warning "目前為 Debug 模式"
#elif RELEASE
#warning "目前為 Release 模式"
#else
#error "無法辨識建置模式"
#endif
#warning 僅輸出警告繼續編譯;#error 輸出錯誤並停止編譯。
Visual Studio 編輯器摺疊區塊,無語意影響,純粹程式碼組織。