views:

247

answers:

5

What is the usage of #if DEBUG pre-processor directive in c#? When must we use this?

+5  A: 

You don't have to use it at all. The purpose of it is to have sections of code that only get compiled in debug mode. e.g. you might have some code that enabled a master user, that could pretend to be any other user in the system for testing and debug pruposes. You would not want that user enabled in the release code for security reasons so you waould wrap the relevent sections of code in #if DEBUG and they would be excluded from release code.

Ben Robinson
Or simply, do not show the splash screen when debugging, saving a second or two. Stuff like that.
TomTom
@TomTom: how about, do not show the splash screen *ever*?
MusiGenesis
+4  A: 

When compiling you can set Compiler flags which you can use to put code into those directives. That code will not be compiled and never ends up in the final assembly output. DEBUG is one of the predefined ones, but you can use your own.

As an example of usage: In one of the current developments we use a compiler flag to state whether to use A login mask to log in a user, or whether login should occur automatically with the current principal. The second mode is onyl for the developers to e.g. debug quicker without having to log in.

Another example: In some mono code you will see flags. In this case code may be compiled differently when e.g. targetting a different framework, as it uses classes that may not exist in earlier releases.

Related to this is the Conditional-Attribute with which you can mark a method. If said flag isn't set, calls to the method will not be performed. The method still ends up in the IL, but calls will be removed.

Check for example following code:

var mthods = typeof (Debug).GetMethods().Where(mi => mi.Name.Equals("WriteLine")).ToList();
var attribs = mthods[0].GetCustomAttributes(true);

You will notice that the Debug.WriteLine method has the Conditional attribute applied to it: Calls to it will be removed when you compile WITHOUT the DEBUG compiler flag.

flq
+4  A: 

You might feel more comfortable with the Conditional attribute, which can be used to exclude entire methods, without needing to complicate the source with conditionals on the calling side:

[Conditional("DEBUG")]
void Foo()
{
}

Foo() can be safely called in both debug and release - however in release mode, it would be a no-op.

stusmith
what's a `no-op`?
runrunraygun
It's a special IL command that essentially say "do nothing". It is especially helpful when you put breakpoints in places where nothing happens (e.g. an open bracket) - that way the debugger can synchronize code and executed IL
flq
+2  A: 

it also comes in handy when testing windows services. You can put in an #if DEBUG to start your process manually, so you don't have to install the service and attach to it to debug.

derek
+2  A: 

In Debug mode:

#if DEBUG
            System.Console.WriteLine("Debug version");
#endif
            System.Console.WriteLine("Output");

Output as

Debug version
Output

In Release mode:

#if DEBUG
            System.Console.WriteLine("Debug version");
#endif
            System.Console.WriteLine("Output");

Output as

   Output

read this: #if (C# Reference)

Useage: If you have a set of values to be tested in the debug mode and not in the release mode you can use #if DEBUG

anishmarokey