欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#預(yù)處理指令分析

 更新時間:2020年08月25日 11:06:35   作者:編碼者頻道  
這篇文章主要介紹了c#預(yù)處理指令的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下

預(yù)處理指令

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

C#中主要的預(yù)處理指令

#define和#undef

#define指令定義:

#define DEBUG

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

#undef定義:

#undef DEBUG

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

注意:

  • 你需要將#define和#undef指令寫在實際業(yè)務(wù)代碼開始之前的位置。
  • #define本身沒有什么用,需要和其他預(yù)處理器指令結(jié)合使用;比如 #if

#if, #elif, #else和#endif

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

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

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

如果編譯器沒發(fā)現(xiàn)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還支持有限的一些邏輯操作符,你可以用使用!,==,!=和||等。

一個標(biāo)記如果存在,則認(rèn)為是true,如果沒有定義,就認(rèn)為是false,因此你也可以這樣使用:

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

#warning和#error

當(dāng)編譯器遇到#warning的時候,會產(chǎn)生警告信息;

當(dāng)編譯器遇到#error的時候,會產(chǎn)生錯誤信息;

  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
    }
  }

編譯結(jié)果:

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

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

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

#line

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

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

#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指令可以用來終止或恢復(fù)某個指定編號到編譯器警告。

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

例如:

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的代碼是區(qū)分大小寫的,CS2019要大寫,如果寫成cs2019則沒有用。

以上就是c#預(yù)處理指令分析的詳細(xì)內(nèi)容,更多關(guān)于c#預(yù)處理指令的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論