C# 沒有獨立前置處理器,前置處理指令作為詞法分析階段的一部分執行,僅為與 C/C++ 相容保留。
# 開頭,位於行首(前可有空白)。#define / #undef
#define DEBUG // 定義符號 DEBUG
#undef DEBUG // 取消定義
限制:必須出現在所有實質程式碼(非指令)之前。
// ❌ 錯誤:using 在 #define 之前
using System;
#define COUNT
// ✅ 正確:#if 非實質程式碼
#define A
#if A
#define B
#endif
namespace N { #if B class C1 {} #endif }
#undef 合法(無作用)。#define NAME=VALUE(C 風格巨集)。