#如果不在C#中调试?


127

我在vb代码行:

#if Not Debug

我必须转换,但在C#中看不到它?

是否有与之等效的解决方案?

Answers:


239

您将需要使用:

#if !DEBUG
    // Your code here
#endif

或者,如果您的符号实际上是 Debug

#if !Debug
    // Your code here
#endif

文档中,您可以有效地将其DEBUG视为布尔值。因此,您可以进行复杂的测试,例如:

#if !DEBUG || (DEBUG && SOMETHING)

15

就像您对这里发生的事情一样熟悉,它#if是一个预处理表达式,并且DEBUG是一个条件编译符号。这是MSDN文章,用于更深入的说明。

默认情况下,在Debug配置中,Visual Studio将检查项目的Build属性下的Define DEBUG constant选项。这适用于C#和VB.NET。如果您想发疯,则可以定义新的构建配置并定义自己的条件编译符号。不过,看到的典型示例是:

#if DEBUG
    //Write to the console
#else
    //write to a file
#endif

10

以防万一它可以帮助别人,这是我的答案。

这将无法正常工作:

#if !DEBUG
     // My stuff here
#endif

但这确实有效:

#if (DEBUG == false)
     // My stuff here
#endif

3
也许在发布之日是正确的,但至少对于VS 2015而言!DEBUG确实按预期工作
Ole Albers

5

我认为类似的东西会起作用

 #if (DEBUG)
//Something
#else
//Something
#endif

5
仅注意此条件#if语句不需要括号。它可以写成#if DEBUG
–conconway,2013年

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.