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

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

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

預(yù)處理指令

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

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

#define和#undef

#define指令定義:

#define DEBUG

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

#undef定義:

#undef DEBUG

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

注意:

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

#if, #elif, #else和#endif

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

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

這段代碼中的Console.Writeline語(yǔ)句,只有在前面用#define指令定義了符號(hào)DEBUG后才會(huì)在編譯的時(shí)候,真正被編譯到;

如果編譯器沒(méi)發(fā)現(xiàn)DEBUG符號(hào),就會(huì)在編譯的時(shí)候忽略這句代碼?!?/p>

#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還支持有限的一些邏輯操作符,你可以用使用!,==,!=和||等。

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

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

#warning和#error

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

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

  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語(yǔ)句是不是做錯(cuò)了什么事,使用#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

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

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

#line

#line指令可以用來(lái)改變編譯器輸出警告和錯(cuò)誤時(shí)相應(yīng)的文件名和行號(hào)信息。這個(gè)實(shí)際中,用的可能會(huì)比較少。

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

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

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

例如:

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

  編譯是會(huì)有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后就不會(huì)有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ū)分大小寫(xiě)的,CS2019要大寫(xiě),如果寫(xiě)成cs2019則沒(méi)有用。

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

相關(guān)文章

最新評(píng)論