C# プリプロセッサ ディレクティブ
C# プリプロセッサ ディレクティブ
このチュートリアルでは、プリプロセッサ ディレクティブ、C# で使用可能なディレクティブ、およびそれらがいつ、なぜ、どのように使用されるのかについて学習します。
名前が示すように、プリプロセッサ ディレクティブは、実際のコンパイルが開始される前に処理されるステートメントのブロックです。 C# プリプロセッサ ディレクティブは、コンパイル プロセスに影響を与えるコンパイラのコマンドです。
これらのコマンドは、コンパイルするコードのセクション、または特定のエラーと警告の処理方法を指定します。
C# プリプロセッサ ディレクティブは # (hash)
で始まります シンボルとすべてのプリプロセッサ ディレクティブは 1 行続きます。プリプロセッサ ディレクティブは new line
で終了します semicolon
ではなく .
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 ディレクティブ
#define
ディレクティブを使用すると、シンボルを定義できます。#if
とともに使用する場合に定義される記号 ディレクティブは true と評価されます。- これらの記号は、コンパイルの条件を指定するために使用できます。
- 構文:
#define SYMBOL
- 例:
#define TESTING
ここで、TESTING はシンボルです。
#undef ディレクティブ
#undef
ディレクティブを使用すると、シンボルを未定義にすることができます。#if
と併用した場合の未定義シンボル ディレクティブは false と評価されます。- 構文:
#undef SYMBOL
- 例:
#undef TESTING
ここで、TESTING はシンボルです。
#if ディレクティブ
#if
ディレクティブは、プリプロセッサ式をテストするために使用されます。- プリプロセッサ式は、シンボルのみ、またはシンボルと
&&
のような演算子の組み合わせで構成されます。 (AND)、||
(または)、!
(ではない) #if
ディレクティブの後には#endif
が続きます 指令。#if
内のコード ディレクティブは、式が#if
でテストされた場合にのみコンパイルされます true と評価されます。- 構文:
#if preprocessor-expression code to compile< #endif
- 例:
#if TESTING Console.WriteLine("Currently Testing"); #endif
例 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 ディレクティブ
#elif
ディレクティブは、複合条件ディレクティブを作成できる #if ディレクティブと一緒に使用されます。- 複数のプリプロセッサ式をテストするときに使用されます。
#elif
内のコード ディレクティブは、式がその#elif
でテストされた場合にのみコンパイルされます true と評価されます。- 構文:
#if preprocessor-expression-1 code to compile #elif preprocessor-expression-2 code-to-compile #endif
- 例:
#if TESTING Console.WriteLine("Currently Testing"); #elif TRAINING Console.WriteLine("Currently Training"); #endif
#else ディレクティブ
#else
ディレクティブは#if
とともに使用されます 指令。- 前の
#if
のいずれの式にも当てはまらない場合 と#elif
(存在する場合) ディレクティブが true の場合、#else
内のコード ディレクティブがコンパイルされます。 - 構文:
#if preprocessor-expression-1 code to compile #elif preprocessor-expression-2 code-to-compile #else code-to-compile #endif
- 例:
#if TESTING Console.WriteLine("Currently Testing"); #elif TRAINING Console.WriteLine("Currently Training"); #else Console.WriteLine("Neither Testing nor Training"); #endif
#endif ディレクティブ
#endif
ディレクティブは#if
とともに使用されます#if
の終わりを示すディレクティブ 指令。- 構文:
#if preprocessor-expression-1 code to compile #endif
- 例:
#if TESTING Console.WriteLine("Currently Testing"); #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 ディレクティブ
#warning
ディレクティブを使用すると、コードからユーザー定義のレベル 1 警告を生成できます。- 構文:
#warning warning-message
- 例:
#warning This is a warning message
例 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 ディレクティブ
#error
ディレクティブを使用すると、コードからユーザー定義のエラーを生成できます。- 構文:
#error error-message
- 例:
#error This is an error message
例 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 ディレクティブ
#line
ディレクティブを使用すると、エラーと警告の行番号とファイル名を変更できます。- 構文:
#line line-number file-name
- 例:
#line 50 "fakeprogram.cs"
例 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 ディレクティブ
#region
ディレクティブを使用すると、Visual Studio コード エディターを使用するときに展開または折りたたむことができる領域を作成できます。- このディレクティブは、単にコードを整理するために使用されます。
- #region ブロックは
#if
と重複できません ブロック。ただし、#region
ブロックは#if
内に含めることができます ブロックと#if
ブロックは#region
とオーバーラップできます ブロックします。 #endregion
ディレクティブは#region
の終わりを示します ブロックします。- 構文:
#region region-description codes #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 ディレクティブ
#pragma
ディレクティブは、それが表示されるファイルをコンパイルするための特別な指示をコンパイラに与えるために使用されます。- 指示には、一部の警告の無効化または有効化が含まれる場合があります。
- C# は 2 つの
#pragma
をサポートします 指示:#pragma warning
:警告の無効化または有効化に使用#pragma checksum
:デバッグに使用されるソース ファイルのチェックサムを生成します。
- 構文:
#pragma pragma-name pragma-arguments
- 例:
#pragma warning disable
例 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言語