Click here to Skip to main content
14,665,550 members
Rate this:
Please Sign up or sign in to vote.
I need to Execute some block of code only if Code is running in Debug Mode.
I dont know how to achieve this, Can you illustrate it ??

I googled Many pages but found none useful.


Rate this:
Please Sign up or sign in to vote.

Solution 3

Please look at the solution by Prerak. Effectively and practically, it probably solves your problem.

However, this is not what you literally asked for. You asked "I need to execute some block of code only if code is running in Debug Mode". If you use the solution pointed out by Prerak, you will get something different: you will be able to execute or not execute some fragment of code, but only because you can have two different builds of you code: one when your fragment is executed, and one when it is not. Once your application is compiled, it will behave either in one or another way.

In other words, you cannot run your application in one or another "mode"; instead, you only can create two different applications which behave differently for two different configuration. One important node: the Debug is only a configuration and has nothing to do with debugging itself. What does have something to do with debugging is the compilation options. You can have as many different configurations as you want; a configuration is just an umbrella for some configuration set under a certain name; you manually configure options for each configuration, and these options may or may not be related to debugging. That said: be careful with the code which executes only in certain configuration; it can be a source of inadequate testing results.

In other words, you cannot determine if a code is running in Debug Mode because such mode does not exist.

Surprise? :-)

Mehdi Gholam 9-Nov-11 23:56pm
Yes, but you can tell if you are running in a debugger.
Sergey Alexandrovich Kryukov 10-Nov-11 11:06am
Detect a debugger? Yes, probably, but I don't know how. Can you explain us?
Prerak Patel 10-Nov-11 3:11am
I agree. The code in Debug directive won't be compiled in Release mode. But I think that how OP wanted it to act.
Sergey Alexandrovich Kryukov 10-Nov-11 11:08am
Thank you, Prerak.
Yes, OP might go your way, but I warned about it. It could be just extra diagnostics or something like that.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Use directives like
    Console.WriteLine("Debug version");
prathameshpitale 9-Nov-11 22:54pm
I kept my mode in #if Debug Directives,
But while code is not in debug mode still it gets executes
Prerak Patel 10-Nov-11 3:12am
You need to build your application in specific mode - Debug or Release. SA has explained it well in detail.
Prerak, my comment may come at surprise: even though your answer is useful in practice, formally it is completely incorrect. Read the question again. There is no such thing: Debug Mode or Release Mode; one can just create different versions of software with different behavior.

Please see my solution.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Sorry, this is not really related to the question. My answer to the question can look a bit surprising. The thing is: there is no Debug or Release modes. Please see my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100