开发者

C# Compiler Directives

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

#if DEBUG
    // Do something here
#else
    // Do somet开发者_高级运维hing 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?


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


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.


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()
{
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜