views:

130

answers:

4

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?

+4  A: 

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.

Skurmedel
**Please** don't post Visual Studio 2003/.NET 1.1 links! Readers follow the links, and then want to follow the links from the links, and wind up stuck in 2003.
John Saunders
Yes sorry, I just fixed that. Silly Google's fault and I have low-bandwidth edition enabled so I don't immediately see. All excuses of course :)
Skurmedel
+2  A: 

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

Kyle Rozendo
So what is the advantage / reasoning behind having these check-boxes rather than having #define DEBUG set?
pm_2
@pm_2 - Centrality. Thats about it.
Kyle Rozendo
+2  A: 

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()
{
}
PoweRoy
+1  A: 

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.

Mr Roys