工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> C言語

C# プリプロセッサ ディレクティブ

C# プリプロセッサ ディレクティブ

このチュートリアルでは、プリプロセッサ ディレクティブ、C# で使用可能なディレクティブ、およびそれらがいつ、なぜ、どのように使用されるのかについて学習します。

名前が示すように、プリプロセッサ ディレクティブは、実際のコンパイルが開始される前に処理されるステートメントのブロックです。 C# プリプロセッサ ディレクティブは、コンパイル プロセスに影響を与えるコンパイラのコマンドです。

これらのコマンドは、コンパイルするコードのセクション、または特定のエラーと警告の処理方法を指定します。

C# プリプロセッサ ディレクティブは # (hash) で始まります シンボルとすべてのプリプロセッサ ディレクティブは 1 行続きます。プリプロセッサ ディレクティブは 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 指令。これらのディレクティブは、テストする条件が複数ある場合に使用されます。また、論理演算子を使用してシンボルを組み合わせて、プリプロセッサ式を形成することもできます。


#warning ディレクティブ


例 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 ディレクティブはプログラムを終了せず、警告をスローするだけです。


#error ディレクティブ

例 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.

おそらく上記のようなエラーが表示されます。ここでは、ユーザー定義のエラーを生成しています。

ここで注意すべきもう 1 つの点は、プログラムが終了し、#error directive example 行が終了することです。 #warning のように印刷されません


#line ディレクティブ

例 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]

2 番目の警告だけが表示されていることがわかります 出力画面に表示されます。

これは、最初の警告の前にすべての警告を最初に無効にし、2 番目の警告の前にのみそれらを復元したためです。これが、最初の警告が非表示になった理由です。

すべての警告ではなく、特定の警告を無効にすることもできます。

#pragma について詳しく知るには 、#pragma (C# リファレンス) にアクセスしてください。


C言語

  1. C# のキーワードと識別子
  2. C++ の変数、リテラル、および定数
  3. C++ データ型
  4. C キーワードと識別子
  5. C データ型
  6. Python のキーワードと識別子
  7. Python正規表現
  8. Python 時間モジュール
  9. 印刷とCNCマシン
  10. 機械加工におけるSFMは何ですか?
  11. C - プリプロセッサ