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

5 篇文章 0 订阅


使用 #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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值