#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
==(같음) 및 !=(같지 않음) 연산자는 bool 값 true 또는 false를 테스트할 때만 사용할 수 있습니다.You can use the operators == (equality) and != (inequality) only to test for the bool values 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). &&(and), ||(or) 및 !(not) 연산자를 사용하여 여러 기호가 정의되었는지 여부를 평가할 수 있습니다.You can use the && (and), || (or), and ! (not) operators 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 지시문으로 전달되는 식으로 해당 기호를 사용하는 경우 식이 true로 평가됩니다.By 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.
빌드 시스템은 SDK 스타일 프로젝트의 여러 대상 프레임워크를 나타내는 미리 정의된 전처리기 기호도 인식합니다.The build system is also aware of predefined preprocessor symbols representing different target frameworks in SDK-style projects. 둘 이상의 .NET 구현 또는 버전을 대상으로 지정할 수 있는 애플리케이션을 만들 때 유용합니다.They're useful when creating applications that can target more than one .NET implementation or version.
| 대상 프레임워크Target 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 표준.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_0, NETCOREAPP3_1NETCOREAPP, NETCOREAPP1_0, NETCOREAPP1_1, NETCOREAPP2_0, NETCOREAPP2_1, NETCOREAPP2_2, NETCOREAPP3_0, NETCOREAPP3_1 |
참고
기존 .NET Framework 프로젝트의 경우 프로젝트의 속성 페이지를 통해 Visual Studio의 여러 대상 프레임워크에 대한 조건부 컴파일 기호를 수동으로 구성해야 합니다.For traditional .NET Framework projects, you have to manually configure the conditional compilation symbols for the different target frameworks in Visual Studio via the project's properties pages.
기타 미리 정의된 기호에는 DEBUG와 TRACE 상수가 있습니다.Other predefined symbols include the DEBUG and TRACE constants. #define을 사용하여 프로젝트에 설정된 값을 재정의할 수 있습니다.You can override the values set for the project using #define. 예를 들어 DEBUG 기호는 빌드 구성 특성(“디버그” 또는 “릴리스” 모드)에 따라 자동으로 설정됩니다.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. 이 예제의 출력은 디버그 구성 모드에서 프로젝트를 빌드했는지, 릴리스 구성 모드에서 프로젝트를 빌드했는지에 따라 다릅니다.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
}
}
다음 예제에서는 가능한 경우 새 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
}
//...
}



