C#預處理器指令 -0016

預處理指令

這些指令/命令不會轉換為可執行代碼,但會影響編譯過程的各個方面;列如,可以讓編譯器不編譯某一部分代碼等。

C#中主要的預處理指令

#define和#undef

#define指令定義

#define DEBUG

它告訴編譯器存在DEBUG這個符號;這個符號不是實際代碼的一部分,而只是在編譯器編譯代碼時候可能會根據這個符號做條件編譯。

#undef定義:

#undef DEBUG

用來移除定義的符號DEBUG。如果不存在這樣的標記,#undef指令則不會生效。同樣,用#define再次定義一個同名的標記也不會有任何變化。

注意:

  1. 你需要將#define和#undef指令寫在實際業務代碼開始之前的位置。
  2. #define本身沒有什麼用,需要和其他預處理器指令結合使用;比如 #if

#if, #elif, #else和#endif

這些指令告訴編譯器是否要編譯包含在其中的代碼塊。例如:

int DoSomeWork(double x)
{
	// do something
	#if DEBUG
		Console.WriteLine($"x is {x}");
	#endif
}

這段代碼中的Console.Writeline語句,只有在前面用#define指令定義了符號DEBUG后才會在編譯的時候,真正被編譯到;

如果編譯器沒發現DEBUG符號,就會在編譯的時候忽略這句代碼。 

#elif(= else if)和#else指令可以用在#if塊中:

#define ENTERPRISE
#define W10
// further on in the file
#if ENTERPRISE
// do something
	#if W10
	// some code that is only relevant to enterprise
	// edition running on W10
	#endif
#elif PROFESSIONAL
// do something else
#else
// code for the leaner version
#endif

#if和#elif還支持有限的一些邏輯操作符,你可以用使用!,==,!=和||等。

一個標記如果存在,則認為是true,如果沒有定義,就認為是false,因此你也可以這樣使用:

#if W10 && (ENTERPRISE==false) // if W10 is defined but ENTERPRISE isn't

 

#warning和#error

 當編譯器遇到#warning的時候,會產生警告信息;

當編譯器遇到#error的時候,會產生錯誤信息;

    class Program
    {
        static void Main(string[] args)
        {

#warning this is a warning message which will be shown when compile

            Console.WriteLine("Hello World!");

#error this is a error message, and will break build
        }
    }

  編譯結果:

Program.cs(10,10): warning CS1030: #warning: 'this is a warning message which will be shown when compile' [/define_warning/define_warning.csproj]
Program.cs(14,8): error CS1029: #error: 'this is a error message, and will break build' [/define_warning/define_warning.csproj]
    1 Warning(s)
    1 Error(s)

  使用這些指令可以檢查#define語句是不是做錯了什麼事,使用#warning可以提醒要做些事情:

#if DEBUG && RELEASE
#error "You've defined DEBUG and RELEASE simultaneously!"
#endif
#warning "Don't forget to remove this line before the boss tests the code!"
Console.WriteLine("*I love this job.*");

  

#region和#endregion

 可以用來標識一段代碼,在Visual Studio或其他能夠識別的IDE里比較有用。

#region Member Field Declarations
int x;
double d;
Currency balance;
#endregion

  

#line

 #line指令可以用來改變編譯器輸出警告和錯誤時相應的文件名和行號信息。這個實際中,用的可能會比較少。

主要是在用第三方包的時候,有時候會導致編譯器報告的行號或文件名與實際不匹配。

#line可以用於還原這種匹配。

#line 164 "Core.cs" // We happen to know this is line 164 in the file Core.cs, 
// before the intermediate package mangles it.
// later on
#line default // restores default line numbering

  

#pragma

 #pragma指令可以用來終止或恢復某個指定編號到編譯器警告。

與命令行選項不同,#pragma指令可以在類或方法級別實現。

例如:

    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            Console.WriteLine("Hello World!");
        }
    }

  編譯是會有warning:

Program.cs(9,17): warning CS0219: The variable 'i' is assigned but its value is never used [/define_warning/define_warning.csproj]
    1 Warning(s)
    0 Error(s)  

 從warning信息可以看出是warning CS0219,加入#pragma后就不會有warning了。

#pragma warning disable CS0219
    public class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            Console.WriteLine("Hello World!");
        }
    }
#pragma warning restore CS0219

 注意:warning的代碼是區分大小寫的,CS2019要大寫,如果寫成cs2019則沒有用。 

 

學習資料

C#高級編程(第11版) C# 7 & .NET Core 2.0(.NET開發經典名著)

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

※為什麼 USB CONNECTOR 是電子產業重要的元件?

網頁設計一頭霧水該從何著手呢? 台北網頁設計公司幫您輕鬆架站!

※台北網頁設計公司全省服務真心推薦

※想知道最厲害的網頁設計公司"嚨底家"!

※推薦評價好的iphone維修中心

您可能也會喜歡…