C#中的条件编译
2013-02-06 08:28
204 查看
条件编译是C#比Java多出的东西,但我跟前辈请教后,他们都说条件编译在实际的项目开发中不怎么使用.鉴于是新内容,我还是做做笔记,理解一下好了.
条件编译属于编译预处理的范畴,它能让我们通过条件编译的机制,将部分代码包括进来或者排除出去,其作用与if-else类似.
条件编译指令有以下四种
#if
#elif
#else
#endif
条件编译指令有以下四种
#if
#elif
#else
#endif
下面我们通一些例子来说明它们的用法
#define Debug // Debugging on
#undef Trace // Tracing off
class PurchaseTransaction
{
void Commit()
{
#if Debug
CheckConsistency();
#if Trace
WriteToLog(this.ToString());
#endif
#endif
CommitHelper();
}
}
#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
{
publicstaticvoid Main()
{
Console.WriteLine(“Demo application”);
}
}
条件编译属于编译预处理的范畴,它能让我们通过条件编译的机制,将部分代码包括进来或者排除出去,其作用与if-else类似.
条件编译指令有以下四种
#if
#elif
#else
#endif
条件编译指令有以下四种
#if
#elif
#else
#endif
下面我们通一些例子来说明它们的用法
#define Debug // Debugging on
#undef Trace // Tracing off
class PurchaseTransaction
{
void Commit()
{
#if Debug
CheckConsistency();
#if Trace
WriteToLog(this.ToString());
#endif
#endif
CommitHelper();
}
}
#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
{
publicstaticvoid Main()
{
Console.WriteLine(“Demo application”);
}
}
相关文章推荐
- C#条件编译,发布多平台和多种选择性的项目
- C# 2.0学习之--条件编译
- C# 2.0学习之--条件编译
- 支持多个版本的AutoCAD——C#中的条件编译——Through the Interface
- C# 条件编译备忘
- C#条件编译选项:Conditional(代替#if...#endif)
- C#条件编译选项:Conditional(代替#if...#endif)
- C#条件编译选项:Conditional(代替#if...#endif)
- C#中的#define条件编译
- c#条件编译
- c#断点无效:"当前不会命中断点。没有与此行关联的可执行代码。可能的原因包括:条件编译或编译器优化"
- C# 条件编译备忘
- C#.NET学习笔记---C#中的条件编译
- C#条件编译选项:Conditional(代替#if...#endif)
- C# #define条件编译详解
- C# 使用Conditional特性而不是#if条件编译
- c#条件编译
- C# 条件编译
- C#的条件编译指令
- c#条件编译