用于调试与发布的 C# if/then 指令 [英] C# if/then directives for debug vs release

查看:37
本文介绍了用于调试与发布的 C# if/then 指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在解决方案属性中,我将我的一个且唯一的项目的配置设置为发布".

In Solution properties, I have Configuration set to "release" for my one and only project.

在主程序的开头,我有这个代码,它显示模式=调试".我在最顶部也有这两行:

At the beginning of the main routine, I have this code, and it is showing "Mode=Debug". I also have these two lines at the very top:

#define DEBUG 
#define RELEASE

我是否在测试正确的变量?

Am I testing the right variable?

#if (DEBUG)
            Console.WriteLine("Mode=Debug"); 
#elif (RELEASE)
            Console.WriteLine("Mode=Release"); 
#endif

我的目标是根据调试与发布模式为变量设置不同的默认值.

My goal is to set different defaults for variables based on debug vs release mode.

推荐答案

DEBUG/_DEBUG 应该已经在 VS 中定义了.

DEBUG/_DEBUG should be defined in VS already.

删除代码中的 #define DEBUG.在构建配置中为该特定构建设置预处理器.

Remove the #define DEBUG in your code. Set preprocessors in the build configuration for that specific build.

它打印Mode=Debug"的原因是因为你的 #define 然后跳过了 elif.

The reason it prints "Mode=Debug" is because of your #define and then skips the elif.

正确的检查方法是:

#if DEBUG
    Console.WriteLine("Mode=Debug"); 
#else
    Console.WriteLine("Mode=Release"); 
#endif

不要检查RELEASE.

这篇关于用于调试与发布的 C# if/then 指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆