快訊 來源:腳本之家 2023-07-07 15:57:56
預處理器指令是指編譯器在實際編譯開始之前對信息進行預處理。通常用于簡化源程序在不同的執行環境中的更改和編譯。例如可以替換文本中的標記,將其他內容插入源文件,或者通過移除幾個部分的文本來取消一部分文件的編譯。不同于 C 和 C++ 中的指令,在 C# 中不能使用這些指令來創建宏,而且預處理器指令必須是一行中唯一的代碼,不能摻雜其它。
示例如下:
#define condition // 定義 condition 字符
using System;
public class ExampleProgram
{
static void Main(string[] args)
{
#if (condition) // 測試 condition 是否為真
Console.WriteLine("condition is defined");
#else
Console.WriteLine("condition is not defined");
#endif
Console.ReadLine();
}
}
在 C# 程序中,所有的預處理器指令都是以標識符 # 開始,例如 #define 和 #if,并且預處理器指令之前只能出現空格不能出現任何代碼。另外,預處理器指令不是語句,因此它們不需要以分號;結尾。
【資料圖】
#nullable 預處理器指令用于設置可為空注釋上下文和為空警告上下文。#nullable 指令控制著是否可為空注釋是否有效,以及是否給出為 Null 的警告。設置著每個上下文要么處于已禁用狀態,要么處于已啟用狀態 。
下表列出 #nullable 指令的用法:
| 用法 | 描述 |
|---|---|
| #nullable disable | 將可為空注釋和警告上下文設置為“已禁用”。 |
| #nullable enable | 將可為空注釋和警告上下文設置為“已啟用”。 |
| #nullable restore | 將可為空注釋和警告上下文還原為項目設置。 |
| #nullable disable annotations | 將可為空注釋上下文設置為“已禁用”。 |
| #nullable enable annotations | 將可為空注釋上下文設置為“已啟用”。 |
| #nullable restore annotations | 將可為空注釋上下文還原為項目設置。 |
| #nullable disable warnings | 將可為空警告上下文設置為“已禁用”。 |
| #nullable enable warnings | 將可為空警告上下文設置為“已啟用”。 |
| #nullable restore warnings | 將可為空警告上下文還原為項目設置。 |
代碼示例:
using System;
public class ExampleProgram
{
static void Main(string[] args)
{
string? str;
#nullable disable // 將可為空注釋和警告上下文設置為“已禁用”。
Console.WriteLine(str); // 報錯: 使用了未賦值的局部變量“str”
#nullable enable // 將可為空注釋和警告上下文設置為“已啟用”。
Console.WriteLine(str);
}
}
代碼界面:(PS:筆者使用的代碼編輯器是 Visual Studio 2022)
可以使用定義符號 #define 和 取消定義符號 #undef 兩個預處理器指令來定義或取消定義條件編譯的符號。定義符號可用于 #if 等編譯指令的條件,使用 #define 來定義符號,將符號用作傳遞給 #if 指令的表達式。
代碼示例:
#define VERBOSE // 定義符 #define
using System;
public class ExampleProgram
{
static void Main(string[] args)
{
#if VERBOSE
Console.WriteLine("詳細輸出版本");
#endif
}
}
代碼執行結果:
詳細輸出版本
可以使用以下四個預處理器指令來控制條件編譯:
#if:打開條件編譯,其中僅在定義了指定的符號時才會編譯代碼。#elif:關閉前面的條件編譯,并基于是否定義了指定的符號打開一個新的條件編譯。#else:關閉前面的條件編譯,如果沒有定義前面指定的符號,打開一個新的條件編譯。#endif:關閉前面的條件編譯。代碼示例:
#define condition2 // 定義 condition 字符
using System;
public class ExampleProgram
{
static void Main(string[] args)
{
#if (condition)
Console.WriteLine("condition is defined");
#elif (condition2) // 測試 condition2 是否為真
Console.WriteLine("condition2 is defined");
#else
Console.WriteLine("condition is not defined");
#endif
Console.ReadLine();
}
}
代碼執行結果:
csharp condition2 is defined
補充:
#if 以及 #else、#elif、#endif、#define 和 #undef 指令,允許在這些指令之間存在一個或多個符號里面包括或排除代碼。其中,#if 指令開頭的條件指令必須以 #endif 指令顯式終止。可以使用#define 指令你定義一個符號,通過將該符號用作傳遞給 #if 指令的表達式。條件編譯指令的用法和 C# 中的條件判斷語句 if、elif 和 else 語句差不多。
可以使用定義區域符號 #region 和 #endregion 分別表示啟動區域和結束區域。這兩個預處理器指令來定義可在大綱中折疊的代碼區域。利用 #region 和 #endregion 指令,可以指定在使用代碼編輯器的大綱功能時可展開或折疊的代碼塊。#region 指令后面可跟折疊區域的名稱。在較長的代碼文件中,折疊或隱藏一個或多個代碼區域十分方便。
代碼示例:
using System;
#region MyClass definition
public class ExampleProgram
{
static void Main(string[] args)
{
}
}
#endregion
折疊前:
折疊后:
可以使用錯誤和警告信息指令告訴編譯器生成用戶定義的編譯器錯誤和警告,并控制行信息。其中包括 #error、#warning 和 #line 指令。
#error:使用指定的消息生成編譯器錯誤。
示例如下:
using System;
public class ExampleProgram
{
static void Main(string[] args)
{
// 錯誤:此方法中的棄用代碼。
#error Deprecated code in this method.
Console.WriteLine("This is Deprecated code");
}
}
代碼界面:
#warning:使用指定的消息生成編譯器警告。
示例如下:
using System;
public class ExampleProgram
{
static void Main(string[] args)
{
// 警告:此方法中的棄用代碼。
#warning Deprecated code in this method.
Console.WriteLine("This is Deprecated code");
}
}
代碼界面:
#line:更改用編譯器消息輸出的行號。
示例如下:
using System;
public class ExampleProgram
{
static void Main()
{
#line 200 "Special"
int i;
int j;
#line default
char c;
float f;
#line hidden // 編號不受影響
string s;
double d;
}
}
編譯產生以下輸出:
#line 200 指令將下一行的行號強制設為 200(盡管默認值為 #6);在執行下一個 #line 指令前,文件名都會報告為“特殊”。#line default 指令將行號恢復至默認行號,這會對上一指令重新編號的行進行計數。#line hidden 指令能對調試程序隱藏連續行,當開發者逐行執行代碼時,介于 #line hidden 和下一 #line 指令(假設它不是其他 #line hidden 指令)間的任何行都將被跳過。Special(200,13): warning CS0168: The variable ‘i’ is declared but never used
Special(201,13): warning CS0168: The variable ‘j’ is declared but never used
MainClass.cs(9,14): warning CS0168: The variable ‘c’ is declared but never used
MainClass.cs(10,15): warning CS0168: The variable ‘f’ is declared but never used
MainClass.cs(12,16): warning CS0168: The variable ‘s’ is declared but never used
MainClass.cs(13,16): warning CS0168: The variable ‘d’ is declared but never used
#pragma 為編譯器給出特殊指令以編譯它所在的文件,這些指令必須受編譯器支持。換句話說,不能使用 #pragma 創建自定義的預處理指令。
#pragma 指令的語法可定義為: #pragma
代碼示例:
using System;
#pragma warning disable 414, CS3021
[CLSCompliant(false)]
public class C
{
int i = 1;
static void Main()
{
}
}
#pragma warning restore CS3021
[CLSCompliant(false)]
public class D
{
int i = 1;
public static void F()
{
}
}
代碼界面:
預處理器指令的用途總結為以下幾點:
有利于項目的調式和運行。例如說可以使用條件編譯指令控制程序流的執行,在實際的項目中表現為多版本代碼片段控制。在代碼的調式階段,可以使用錯誤和警告信息指令來禁止編譯不屬于本功能的額外代碼。使用定義區域指令可以很好折疊和隱藏指定區域的代碼片段。開發者可以更好的集中處理關鍵代碼,在有著多個代碼區域的項目十分的方便。到此這篇關于C# 預處理器指令的用法的文章就介紹到這了,更多相關C# 預處理器指令內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
標簽:
免責聲明:市場有風險,選擇需謹慎!此文僅供參考,不作買賣依據。
下一篇:最后一頁