#if (C# 參考)#if (C# Reference)

當 C# 編譯器遇到 #if 指示詞,且其後接著 #endif 指示詞時,只有在定義了指定的符號時,它才會編譯指示詞之間的程式碼。When the C# compiler encounters an #if directive, followed eventually by an #endif directive, it compiles the code between the directives only if the specified symbol is defined. 不同於 C 和 C++,您無法將數值指派給符號。Unlike C and C++, you cannot assign a numeric value to a symbol. C# 中的 #if 陳述式是布林值,且只測試是否已經定義符號。The #if statement in C# is Boolean and only tests whether the symbol has been defined or not. 例如:For example:

#if DEBUG
    Console.WriteLine("Debug version");
#endif

您只能使用運算子 == (相等) 和 != (不等) 來測試 truefalseYou can use the operators == (equality) and != (inequality) only to test for true or false. True 表示已定義符號。True means the symbol is defined. #if DEBUG 陳述式的意義與 #if (DEBUG == true) 一樣。The statement #if DEBUG has the same meaning as #if (DEBUG == true). 您可以使用運算子 && (且)、|| (或) 及 !You can use the operators && (and), || (or), and ! (非),來評估是否已定義多個符號。(not) to evaluate whether multiple symbols have been defined. 您也可以使用括弧來將符號和運算子分組。You can also group symbols and operators with parentheses.

備註Remarks

#if 連同 #else#elif#endif#define#undef 指示詞,可讓您根據一或多個符號是否存在來包含或排除程式碼。#if, along with the #else, #elif, #endif, #define, and #undef directives, lets you include or exclude code based on the existence of one or more symbols. 在編譯偵錯組建的程式碼時,或是在針對特定組態進行編譯時,這非常實用。This can be useful when compiling code for a debug build or when compiling for a specific configuration.

#if 指示詞開頭的條件式指示詞必須明確地以 #endif 指示詞終止。A conditional directive beginning with a #if directive must explicitly be terminated with a #endif directive.

#define 可讓您定義符號。#define lets you define a symbol. 屆時,使用該符號作為傳遞到 #if 指示詞的運算式,運算式就會評估為 trueBy then using the symbol as the expression passed to the #if directive, the expression evaluates to true.

您也可以使用 -define 編譯器選項來定義符號。You can also define a symbol with the -define compiler option. 您可以使用 #undef 來取消定義符號。You can undefine a symbol with #undef.

透過 -define#define 定義的符號不會與相同名稱的變數發生衝突。A symbol that you define with -define or with #define doesn't conflict with a variable of the same name. 也就是說,您不應將變數名稱傳遞給前置處理器指示詞,而且符號僅能由前置處理器指示詞來評估。That is, a variable name should not be passed to a preprocessor directive, and a symbol can only be evaluated by a preprocessor directive.

使用 #define 建立的符號範圍是定義它的檔案。The scope of a symbol created with #define is the file in which it was defined.

建置系統也能分辨代表不同目標 Framework 的預先定義前置處理器符號。The build system is also aware of predefined preprocessor symbols representing different target frameworks. 若要建立能以多個 .NET 實作或版本為目標的應用程式,這些符號就很實用。They're useful when creating applications that can target more than one .NET implementation or version.

目標 FrameworkTarget Frameworks 符號Symbols
.NET Framework.NET Framework NETFRAMEWORK, NET20, NET35, NET40, NET45, NET451, NET452, NET46, NET461, NET462, NET47, NET471, NET472, NET48NETFRAMEWORK, NET20, NET35, NET40, NET45, NET451, NET452, NET46, NET461, NET462, NET47, NET471, NET472, NET48
.NET Standard.NET Standard NETSTANDARD, NETSTANDARD1_0, NETSTANDARD1_1, NETSTANDARD1_2, NETSTANDARD1_3, NETSTANDARD1_4, NETSTANDARD1_5, NETSTANDARD1_6, NETSTANDARD2_0, NETSTANDARD2_1NETSTANDARD, NETSTANDARD1_0, NETSTANDARD1_1, NETSTANDARD1_2, NETSTANDARD1_3, NETSTANDARD1_4, NETSTANDARD1_5, NETSTANDARD1_6, NETSTANDARD2_0, NETSTANDARD2_1
.NET Core.NET Core NETCOREAPP, NETCOREAPP1_0, NETCOREAPP1_1, NETCOREAPP2_0, NETCOREAPP2_1, NETCOREAPP2_2, NETCOREAPP3_0NETCOREAPP, NETCOREAPP1_0, NETCOREAPP1_1, NETCOREAPP2_0, NETCOREAPP2_1, NETCOREAPP2_2, NETCOREAPP3_0

其他預先定義符號包括 DEBUG 和 TRACE 常數。Other predefined symbols include the DEBUG and TRACE constants. 您可以使用 #define 來覆寫為專案所設定的值。You can override the values set for the project using #define. 例如,DEBUG 符號會根據您的組建組態屬性 ("Debug" 或 "Release" 模式) 而自動設定。The DEBUG symbol, for example, is automatically set depending on your build configuration properties ("Debug" or "Release" mode).

範例Examples

以下範例說明如何在檔案上定義 MYTEST 符號,然後測試 MYTEST 和 DEBUG 符號的值。The following example shows you how to define a MYTEST symbol on a file and then test the values of the MYTEST and DEBUG symbols. 此範例的輸出視您使用 Debug 或 Release 組態模式建置專案而有所不同。The output of this example depends on whether you built the project on Debug or Release configuration mode.

#define MYTEST
using System;
public class MyClass
{
    static void Main()
    {
#if (DEBUG && !MYTEST)
        Console.WriteLine("DEBUG is defined");
#elif (!DEBUG && MYTEST)
        Console.WriteLine("MYTEST is defined");
#elif (DEBUG && MYTEST)
        Console.WriteLine("DEBUG and MYTEST are defined");  
#else
        Console.WriteLine("DEBUG and MYTEST are not defined");
#endif
    }
}

以下範例說明如何測試不同的目標 Framework,讓您可以盡可能使用較新的 API:The following example shows you how to test for different target frameworks so you can use newer APIs when possible:

public class MyClass
{
    static void Main()
    {
#if NET40
        WebClient _client = new WebClient();
#else
        HttpClient _client = new HttpClient();
#endif
    }
    //...
}

另請參閱See also