산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> C 언어

C# 전처리기 지시문

C# 전처리기 지시문

이 튜토리얼에서는 전처리기 지시문, C#에서 사용 가능한 지시문, 그리고 언제, 왜, 어떻게 사용되는지 알아보겠습니다.

이름에서 알 수 있듯이 전처리기 지시문은 실제 컴파일이 시작되기 전에 처리되는 명령문 블록입니다. C# 전처리기 지시문은 컴파일 프로세스에 영향을 주는 컴파일러용 명령입니다.

이 명령은 컴파일할 코드 섹션 또는 특정 오류 및 경고를 처리하는 방법을 지정합니다.

C# 전처리기 지시문은 # (hash)로 시작합니다. 기호 및 모든 전처리기 지시문은 한 줄 동안 지속됩니다. 전처리기 지시문은 new line로 종료됩니다. semicolon가 아닌 .

C#에서 사용할 수 있는 전처리기 지시문은 다음과 같습니다.

C#의 전처리기 지시문
전처리기 지침 설명 구문
#if 전처리기 표현식이 참인지 아닌지 확인
#if preprocessor-expression
	code to compile
#endif
#elif #if과 함께 사용 여러 전처리기 표현식을 확인하려면
#if preprocessor-expression-1
	code to compile
#elif preprocessor-expression-2
	code to compile
#endif
#else #if와 함께 사용 복합 조건부 지시문을 생성합니다.
#if preprocessor-expression
	code to compile
#elif
	code to compile
#endif
#endif #if과 함께 사용 조건부 지시문의 끝을 나타내기 위해
#if preprocessor-expression
	code to compile
#endif
#define 기호를 정의하는 데 사용
#define SYMBOL
#undef 기호 정의를 해제하는 데 사용됨
#undef SYMBOL
#warning 코드에서 레벨 1 경고를 생성할 수 있습니다.
#warning warning-message
#error 코드에서 오류를 생성하도록 허용
#error error-message
#line 오류 및 경고를 표시하기 위해 컴파일러의 줄 번호와 파일 이름을 수정할 수 있습니다.
#line line-number file-name
#region Visual Studio Code Editor를 사용할 때 확장하거나 축소할 수 있는 영역을 만들 수 있습니다.
#region region-description
	codes
#endregion
#endregion 영역의 끝을 나타냅니다.
#region region-description
	codes
#endregion
#pragma 컴파일러에 파일이 있는 파일의 컴파일을 위한 특수 지침을 제공합니다.
#pragma pragma-name pragma-arguments
<시간>

#define 지시문

<시간>

#undef 지시문

<시간>

#if 지시문

예시 1:#if 지시문을 사용하는 방법

#define CSHARP

using System;
 
namespace Directive
{
	class ConditionalDirective
	{
		public static void Main(string[] args)
		{
			#if (CSHARP)
				Console.WriteLine("CSHARP is defined");
			#endif
		}
	}
}

프로그램을 실행하면 다음과 같이 출력됩니다.

CSHARP is defined

위 프로그램에서 CSHARP 기호는 #define를 사용하여 정의됩니다. 프로그램 시작 시 지시문. Main() 내부 메소드, #if 지시문은 CSHARP 여부를 테스트하는 데 사용됩니다. 사실인지 아닌지. #if 내부의 코드 블록 지시문은 CSHARP인 경우에만 컴파일됩니다. 정의됩니다.

<시간>

#elif 지시문

<시간>

#else 지시문

<시간>

#endif 지시문

예시 2:조건부 지시문(if, elif, else, endif)을 사용하는 방법 ?

#define CSHARP
#undef PYTHON
 
using System;
 
namespace Directive
{
	class ConditionalDirective
	{
		static void Main(string[] args)
		{
			#if (CSHARP && PYTHON)
				Console.WriteLine("CSHARP and PYTHON are defined");
			#elif (CSHARP && !PYTHON)
				Console.WriteLine("CSHARP is defined, PYTHON is undefined");
			#elif (!CSHARP && PYTHON)
				Console.WriteLine("PYTHON is defined, CSHARP is undefined");
			#else
				Console.WriteLine("CSHARP and PYTHON are undefined");
			#endif
		}
	}
}

프로그램을 실행하면 다음과 같이 출력됩니다.

CSHARP is defined, PYTHON is undefined

이 예에서는 #elif의 사용을 볼 수 있습니다. 및 #else 지령. 이 지시문은 테스트할 여러 조건이 있을 때 사용됩니다. 또한 논리 연산자를 사용하여 기호를 결합하여 전처리기 표현식을 구성할 수 있습니다.

<시간>

#경고 지시문

<시간>

예시 3:#warning 지시문을 사용하는 방법

using System;
 
namespace Directives
{
	class WarningDirective
	{
		public static void Main(string[] args)
		{
			#if (!CSHARP)
				#warning CSHARP is undefined
			#endif
			Console.WriteLine("#warning directive example");
		}
	}
}

프로그램을 실행하면 다음과 같이 출력됩니다.

Program.cs(10,26): warning CS1030: #warning: 'CSHARP is undefined' [/home/myuser/csharp/directives-project/directives-project.csproj]
#warning directive example

위의 프로그램을 실행하면 위와 같은 출력을 볼 수 있습니다. 텍스트는 경고 메시지를 나타냅니다. 여기서 #warning를 사용하여 사용자 정의 경고 메시지를 생성합니다. 지시.

#warning 뒤의 문장은 지시문도 실행됩니다. #warning 지시문은 프로그램을 종료하지 않고 경고만 발생시킵니다.

<시간>

#오류 지시문

예시 4:#error 지시문은 어떻게 사용하나요?

using System;
 
namespace Directive
{
	class Error
	{
		public static void Main(string[] args)
		{
			#if (!CSHARP)
				#error CSHARP is undefined
			#endif
			Console.WriteLine("#error directive example");
		}
	}
}

프로그램을 실행하면 다음과 같이 출력됩니다.

Program.cs(10,24): error CS1029: #error: 'CSHARP is undefined' [/home/myuser/csharp/directives-project/directives-project.csproj]
The build failed. Please fix the build errors and run again.

위와 같은 몇 가지 오류가 표시됩니다. 여기서 사용자 정의 오류가 생성됩니다.

여기서 주목해야 할 또 다른 사항은 프로그램이 종료되고 #error directive example #warning과 같이 인쇄되지 않습니다. 지시.

<시간>

#라인 지시문

예시 5:#line 지시문을 사용하는 방법

using System;
 
namespace Directive
{
	class Error
	{
		public static void Main(string[] args)
		{
			#line 200 "AnotherProgram.cs"
			#warning Actual Warning generated by Program.cs on line 10
		}
	}
}

프로그램을 실행하면 다음과 같이 출력됩니다.

AnotherProgram.cs(200,22): warning CS1030: #warning: 'Actual Warning generated by Program.cs on line 10' [/home/myuser/csh
arp/directive-project/directive-project.csproj]

위의 예를 Program.cs으로 저장했습니다. . 경고는 실제로 line 10에서 생성되었습니다. Program.cs 기준 . #line 사용 지시문에 따라 줄 번호를 200으로 변경했습니다. 파일 이름은 AnotherProgram.cs입니다. 오류가 발생했습니다.

<시간>

#region 및 #endregion 지시문

예시 6:#region 지시문을 사용하는 방법

using System;
 
namespace Directive
{
	class Region
	{
		public static void Main(string[] args)
		{
			#region Hello
			Console.WriteLine("Hello");
			Console.WriteLine("Hello");
			Console.WriteLine("Hello");
			Console.WriteLine("Hello");
			Console.WriteLine("Hello");
			#endregion
		}
	}
}

프로그램을 실행하면 다음과 같이 출력됩니다.

Hello
Hello
Hello
Hello
Hello
<시간>

#pragma 지시문

예시 7:#pragma 지시문을 사용하는 방법

using System;
 
namespace Directive
{
	class Error
	{
		public static void Main(string[] args)
		{
			#pragma warning disable
			#warning This is a warning 1
			#pragma warning restore
			#warning This is a warning 2
		}
	}
}

프로그램을 실행하면 다음과 같이 출력됩니다.

Program.cs(12,22): warning CS1030: #warning: 'This is a warning 2' [/home/myuser/csharp/directive-project/directive-project.csproj]

두 번째 경고만 출력 화면에 표시됩니다.

이는 처음에 첫 번째 경고 전에 모든 경고를 비활성화하고 두 번째 경고 전에만 복원했기 때문입니다. 이것이 첫 번째 경고를 숨긴 이유입니다.

모든 경고 대신 특정 경고를 비활성화할 수도 있습니다.

#pragma에 대해 자세히 알아보려면 , #pragma(C# 참조)를 방문하세요.


C 언어

  1. C# 키워드 및 식별자
  2. C++ 변수, 리터럴 및 상수
  3. C++ 데이터 유형
  4. C 키워드 및 식별자
  5. C 데이터 유형
  6. 파이썬 키워드와 식별자
  7. 파이썬 정규식
  8. 파이썬 시간 모듈
  9. 인쇄 대 CNC 기계
  10. 가공에서 SFM이란 무엇입니까?