您的位置:首页 > 其它

#if #elif #endif 的使用--------条件编译(conditional compilation)

2014-01-03 22:13 489 查看

使用 #if 以及
#else、#elif#endif#define

#undef 指令,可以包括或排除基于由一个或多个符号组成的条件的代码

一般条件编译用于你在调试的时候,需要输出结果来确定程序是否正确,但在发布的时候不需要输出这些结果。或者程序在某一版本发布了一个试用的功能,但在下一版本中要暂时去掉这个功能,就可以这个条件来控制。

#define DEBUG
#define MYTEST
using System;
namespace ConsoleApplication1
{
public class Class1
{
public void Display()
{
#if (DEBUG && !MYTEST)
Console.WriteLine("DEBUG is defined");
#elif (!DEBUG && MYTEST)
Console.WriteLine("MYTEST is defined");
#elif (DEBUG && MYTEST)
Console.WriteLine("DEBUG and MYTEST are defined");
#else
Console.WriteLine("DEBUG and MYTEST are not defined");
#endif
}
}
}

using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Class1 cl = new Class1();
cl.Display();
#if DEBUG
Console.WriteLine("123");
#endif
}
}
}


结果

DEBUG and VC_V7 are defined

123
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: