<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # C# 預處理器指令 > 原文: [https://www.programiz.com/csharp-programming/preprocessor-directives](https://www.programiz.com/csharp-programming/preprocessor-directives) #### 在本教程中,我們將學習預處理器指令,C# 中的可用指令以及何時,為什么以及為什么使用它們。 顧名思義,預處理器指令是在實際編譯開始之前進行處理的語句塊。 C# 預處理器指令是影響編譯過程的編譯器命令。 這些命令指定要編譯的代碼部分或如何處理特定的錯誤和警告。 C# 預處理器指令以`# (hash)`符號開頭,所有預處理器指令都持續一行。 預處理器指令由`new line`而不是`semicolon`終止。 C# 中可用的預處理器指令為: Preprocessor directives in C# `#if` 檢查預處理器表達式是否為真 ```cs #if preprocessor-expression code to compile #endif ``` `#elif` 與`#if`一起使用以檢查多個預處理器表達式 ```cs #if preprocessor-expression-1 code to compile #elif preprocessor-expression-2 code to compile #endif ``` `#else` 與`#if`一起使用以創建復合條件指令。 ```cs #if preprocessor-expression code to compile #elif code to compile #endif ``` `#endif` 與`#if`一起使用以指示條件指令的結尾 ```cs #if preprocessor-expression code to compile #endif ``` `#define` 用于定義符號 ```cs #define SYMBOL ``` `#undef` 用于取消定義符號 ```cs #undef SYMBOL ``` `#warning` 允許我們從代碼中生成 1 級警告 ```cs #warning warning-message ``` `#error` 允許我們從代碼中生成錯誤 ```cs #error error-message ``` `#line` 允許我們修改編譯器的行號和文件名以顯示錯誤和警告 ```cs #line line-number file-name ``` `#region` 允許我們創建一個使用 Visual Studio 代碼編輯器時可以擴展或折疊的區域 ```cs #region region-description codes #endregion ``` `#endregion` 指示區域的結尾 ```cs #region region-description codes #endregion ``` `#pragma` 為編譯器提供特別的說明,以編譯其中出現的文件。 ```cs #pragma pragma-name pragma-arguments ``` * * * ## `#define`指令 * `#define`指令允許我們定義符號。 * 與`#if`偽指令一起使用時定義的符號將求值為`true`。 * 這些符號可用于指定編譯條件。 * **語法**: ```cs #define SYMBOL ``` * **例如**: ```cs #define TESTING ``` 在這里,`TESTING`是一個符號。 * * * ## `#undef`指令 * `#undef`指令允許我們取消定義符號。 * 與`#if`偽指令一起使用時,未定義的符號將計算為`false`。 * **語法**: ```cs #undef SYMBOL ``` * 例如: ```cs #undef TESTING ``` 在這里,`TESTING`是一個符號。 * * * ## `#if`指令 * `#if`指令用于測試預處理器表達式。 * 預處理器表達式可以僅由符號組成,也可以由符號的組合以及`&&`(AND),`||`(OR),`!`(NOT)等運算符組成。 * `#if`指令后跟`#endif`指令。 * 僅當使用`#if`測試的表達式的值為`true`時,才會編譯`#if`指令中的代碼。 * **語法**: ```cs #if preprocessor-expression code to compile< #endif ``` * **例如**: ```cs #if TESTING Console.WriteLine("Currently Testing"); #endif ``` ### 示例 1:如何使用`#if`指令? ```cs #define CSHARP using System; namespace Directive { class ConditionalDirective { public static void Main(string[] args) { #if (CSHARP) Console.WriteLine("CSHARP is defined"); #endif } } } ``` 當我們運行程序時,輸出將是: ```cs CSHARP is defined ``` 在上述程序中,`CSHARP`符號是在程序開頭使用`#define`指令定義的。 在`Main()`方法內部,`#if`指令用于測試`CSHARP`是否為`true`。 僅當定義了`CSHARP`時,才編譯`#if`指令內的代碼塊。 * * * ## `#elif`指令 * `#elif`指令與`#if`指令一起使用,可讓我們創建復合條件指令。 * 在測試多個預處理器表達式時使用它。 * 僅當用`#elif`測試的表達式的值為真時,才編譯`#elif`指令中的代碼。 * **語法**: ```cs #if preprocessor-expression-1 code to compile #elif preprocessor-expression-2 code-to-compile #endif ``` * **例如**: ```cs #if TESTING Console.WriteLine("Currently Testing"); #elif TRAINING Console.WriteLine("Currently Training"); #endif ``` * * * ## `#else`指令 * `#else`指令與`#if`指令一起使用。 * 如果前面的`#if`和`#elif`指令(如果存在)中的表達式都不為真,則將編譯`#else`指令中的代碼。 * **語法**: ```cs #if preprocessor-expression-1 code to compile #elif preprocessor-expression-2 code-to-compile #else code-to-compile #endif ``` * **例如**: ```cs #if TESTING Console.WriteLine("Currently Testing"); #elif TRAINING Console.WriteLine("Currently Training"); #else Console.WriteLine("Neither Testing nor Training"); #endif ``` * * * ## `#endif`指令 * `#endif`指令與`#if`指令一起使用以指示`#if`指令的結尾。 * **語法**: ```cs #if preprocessor-expression-1 code to compile #endif ``` * **例如**: ```cs #if TESTING Console.WriteLine("Currently Testing"); #endif ``` ### 示例 2:如何使用條件指令(`if`,`elif`,`else`,`endif`)? ```cs #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 } } } ``` 當我們運行程序時,輸出將是: ```cs CSHARP is defined, PYTHON is undefined ``` 在此示例中,我們可以看到`#elif`和`#else`指令的使用。 當有多個條件要測試時,將使用這些指令。 同樣,可以使用邏輯運算符組合符號以形成預處理器表達式。 * * * ## `#warning`指令 * `#warning`指令允許我們從代碼中生成用戶定義的一級警告。 * **語法**: ```cs #warning warning-message ``` * **例如**: ```cs #warning This is a warning message ``` * * * ### 示例 3:如何使用`#warning`指令? ```cs using System; namespace Directives { class WarningDirective { public static void Main(string[] args) { #if (!CSHARP) #warning CSHARP is undefined #endif Console.WriteLine("#warning directive example"); } } } ``` 當我們運行程序時,輸出將是: ```cs 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`指令允許我們從代碼中生成用戶定義的錯誤。 * **語法**: ```cs #error error-message ``` * **例如**: ```cs #error This is an error message ``` ### 示例 4:如何使用`#error`指令? ```cs using System; namespace Directive { class Error { public static void Main(string[] args) { #if (!CSHARP) #error CSHARP is undefined #endif Console.WriteLine("#error directive example"); } } } ``` 當我們運行程序時,輸出將是: ```cs 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. ``` 我們將看到一些錯誤,可能與上面類似。 在這里,我們正在生成用戶定義的錯誤。 這里要注意的另一件事是程序將終止并且不會像在`#warning`指令中那樣打印`#error directive example`行。 * * * ## `#line`指令 * `#line`指令允許我們修改行號和文件名以獲取錯誤和警告。 * **語法**: ```cs #line line-number file-name ``` * **例如**: ```cs #line 50 "fakeprogram.cs" ``` ### 示例 5:如何使用`#line`指令? ```cs 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 } } } ``` 當我們運行程序時,輸出將是: ```cs 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`。 該警告實際上是由`Program.cs`在`line 10`生成的。 使用`#line`指令,我們將行號更改為`200`,并將文件名更改為`AnotherProgram.cs`,從而產生了錯誤。 * * * ## `#region`和`#endregion`指令 * `#region`指令允許我們創建一個使用 Visual Studio 代碼編輯器時可以擴展或折疊的區域。 * 該指令僅用于組織代碼。 * `#region`塊不能與`#if`塊重疊。 但是,`#region`塊可以包含在`#if`塊內,并且`#if`塊可以與`#region`塊重疊。 * `#endregion`指令指示`#region`塊的結尾。 * **語法**: ```cs #region region-description codes #endregion ``` ### 示例 6:如何使用`#region`指令? ```cs 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 } } } ``` 當我們運行程序時,輸出將是: ```cs Hello Hello Hello Hello Hello ``` * * * ## `#pragma`指令 * `#pragma`指令用于為編譯器提供一些特殊的指令,用于編譯該文件所在的文件。 * 該指令可以包括禁用或啟用某些警告。 * C# 支持兩條`#pragma`指令: * `#pragma warning`:用于禁用或啟用警告 * `#pragma checksum`:它生成用于調試的源文件的校驗和。 * **語法**: ```cs #pragma pragma-name pragma-arguments ``` * **例如**: ```cs #pragma warning disable ``` ### 示例 7:如何使用`#pragma`指令? ```cs 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 } } } ``` 當我們運行程序時,輸出將是: ```cs Program.cs(12,22): warning CS1030: #warning: 'This is a warning 2' [/home/myuser/csharp/directive-project/directive-project.csproj] ``` 我們可以看到在輸出屏幕上僅顯示**第二個警告**。 這是因為,我們最初禁用了第一個警告之前的所有警告,而僅在第二個警告之前將它們還原。 這就是隱藏第一個警告的原因。 我們還可以禁用特定警告而不是所有警告。 要了解有關`#pragma`的更多信息,請訪問[`#pragma`(C# 參考)](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-pragma "#pragma reference C#")。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看