條件編譯指令 依符號定義狀態,決定原始碼區塊是否參與編譯。
#if(必要)#elif#else#endif(必要)&&(AND)、||(OR)、!(NOT)。
#define A
#define B
#undef C
class D {
#if C
void F() {}
#elif A && B
void I() {} // 實際編譯進入
#else
void G() {}
#endif
}
#define DEBUG
#undef TRACE
class Transaction {
void Commit() {
#if DEBUG
CheckConsistency();
#if TRACE
WriteToLog(this.ToString());
#endif
#endif
CommitHelper();
}
}
Console.WriteLine(@"hello, #if DEBUG world #else everyone #endif ");
// 輸出原文,指令不在字串中執行