C#.NET學(xué)習(xí)筆記5 C#中的條件編譯
更新時(shí)間:2012年11月01日 21:41:40 作者:
條件編譯是C#比Java多出的東西,但我跟前輩請(qǐng)教后,他們都說條件編譯在實(shí)際的項(xiàng)目開發(fā)中不怎么使用.鑒于是新內(nèi)容,我還是做做筆記,理解一下好了
條件編譯是C#比Java多出的東西,但我跟前輩請(qǐng)教后,他們都說條件編譯在實(shí)際的項(xiàng)目開發(fā)中不怎么使用.鑒于是新內(nèi)容,我還是做做筆記,理解一下好了.
條件編譯屬于編譯預(yù)處理的范疇,它能讓我們通過條件編譯的機(jī)制,將部分代碼包括進(jìn)來或者排除出去,其作用與if-else類似.
條件編譯指令有以下四種 #if #elif #else #endif 條件編譯指令有以下四種
#if
#elif
#else
#endif
下面我們通一些例子來說明它們的用法
#define Debug
class Class1
{
#if Debug
void Trace(string s) {}
#endif
}
執(zhí)行時(shí)由于第一行已經(jīng)使用#define 指令定義了符號(hào)Debug, #if 的條件滿足,所以這段代碼等同于
class Class1
{
void Trace(string s) {}
}
再比如:
#define A
#define B
#undef C
class D
{
#if C
void F() {}
#elif A && B
void I() {}
#else
void G() {}
#endif
}
其編譯效果等同于:
class C
{
void I() {}
}
#if 指令可以嵌套使用, 例如:
#define Debug // Debugging on
#undef Trace // Tracing off
class PurchaseTransaction
{
void Commit()
{
#if Debug
CheckConsistency();
#if Trace
WriteToLog(this.ToString());
#endif
#endif
CommitHelper();
}
}
預(yù)編譯和條件編譯指令還可以幫助我們?cè)诔绦驁?zhí)行過程中發(fā)出編譯的錯(cuò)誤或警告,相應(yīng)的指令是#warning 和#error,下面的程序展示了它們的用法:
#define DEBUG
#define RELEASE
#define DEMO VERSION
#if DEMO VERSION && !DEBUG
#warning you are building a demo version
#endif
#if DEBUG && DEMO VERSION
#error you cannot build a debug demo version
#endif
using System;
class Demo
{
public static void Main()
{
Console.WriteLine(“Demo application”);
}
}
作者:notifier
條件編譯屬于編譯預(yù)處理的范疇,它能讓我們通過條件編譯的機(jī)制,將部分代碼包括進(jìn)來或者排除出去,其作用與if-else類似.
條件編譯指令有以下四種 #if #elif #else #endif 條件編譯指令有以下四種
#if
#elif
#else
#endif
下面我們通一些例子來說明它們的用法
復(fù)制代碼 代碼如下:
#define Debug
class Class1
{
#if Debug
void Trace(string s) {}
#endif
}
執(zhí)行時(shí)由于第一行已經(jīng)使用#define 指令定義了符號(hào)Debug, #if 的條件滿足,所以這段代碼等同于
復(fù)制代碼 代碼如下:
class Class1
{
void Trace(string s) {}
}
再比如:
復(fù)制代碼 代碼如下:
#define A
#define B
#undef C
class D
{
#if C
void F() {}
#elif A && B
void I() {}
#else
void G() {}
#endif
}
其編譯效果等同于:
復(fù)制代碼 代碼如下:
class C
{
void I() {}
}
#if 指令可以嵌套使用, 例如:
復(fù)制代碼 代碼如下:
#define Debug // Debugging on
#undef Trace // Tracing off
class PurchaseTransaction
{
void Commit()
{
#if Debug
CheckConsistency();
#if Trace
WriteToLog(this.ToString());
#endif
#endif
CommitHelper();
}
}
預(yù)編譯和條件編譯指令還可以幫助我們?cè)诔绦驁?zhí)行過程中發(fā)出編譯的錯(cuò)誤或警告,相應(yīng)的指令是#warning 和#error,下面的程序展示了它們的用法:
復(fù)制代碼 代碼如下:
#define DEBUG
#define RELEASE
#define DEMO VERSION
#if DEMO VERSION && !DEBUG
#warning you are building a demo version
#endif
#if DEBUG && DEMO VERSION
#error you cannot build a debug demo version
#endif
using System;
class Demo
{
public static void Main()
{
Console.WriteLine(“Demo application”);
}
}
作者:notifier
相關(guān)文章
C#利用WMI操作DNS服務(wù)器(可遠(yuǎn)程操作,需要相應(yīng)權(quán)限)
C#利用WMI操作DNS服務(wù)器(可遠(yuǎn)程操作,需要相應(yīng)權(quán)限)...2007-03-03
Unity調(diào)用手機(jī)攝像機(jī)識(shí)別二維碼
這篇文章主要為大家詳細(xì)介紹了Unity調(diào)用手機(jī)攝像機(jī)識(shí)別二維碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
在Unity中實(shí)現(xiàn)動(dòng)畫的正反播放代碼
這篇文章主要介紹了在Unity中實(shí)現(xiàn)動(dòng)畫的正反播放代碼,非常的實(shí)用,這里推薦給大家,希望大家能夠喜歡。2015-03-03
C#結(jié)合Minio實(shí)現(xiàn)文件上傳存儲(chǔ)與更新
MinIO是一個(gè)開源的對(duì)象存儲(chǔ)服務(wù)器,專門設(shè)計(jì)用于在大規(guī)模數(shù)據(jù)存儲(chǔ)環(huán)境中運(yùn)行,這篇文章主要為大家介紹了C#如何結(jié)合Minio實(shí)現(xiàn)文件上傳存儲(chǔ)與更新,需要的可以參考下2024-03-03

