C# Compiler Directives

自闭症网瘾萝莉.ら 提交于 2019-12-01 15:01:11

问题


I’m looking at some C# code, and have come across the following statement:

#if DEBUG
    // Do something here
#else
    // Do something else
#endif

I assumed that DEBUG would be a defined somewhere as follows:

#define DEBUG

But I’m unable to find such a definition, although the code seems to behave as though it were set. Is DEBUG a special case, and if so, how is it set / unset?


回答1:


On the project, go to Properties -> Build. Under general, you have an option there for defining both DEBUG and TRACE.




回答2:


It is set with the #define directive or in the compiler settings. It is common for DEBUG to be defined in debug releases, so you could conditionally compile some code like in your example.

You can read more about it on MSDN.




回答3:


If you look in the project properties you will find a debug option DEBUG Then you can do in C#:

[Conditional("Debug")]
public void DebugThis()
{
}



回答4:


You can also define the DEBUG and TRACE conditional compilation constants under the project Properties' Build tab. For this instance, Define DEBUG constant checkbox is probably checked for your project.

More details @ MSDN.



来源:https://stackoverflow.com/questions/2920239/c-sharp-compiler-directives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!