Question about "#if DEBUG"

lapucca
lapucca used Ask the Experts™
on
Hi, I'm using VS2013, C# and asp.net web form
I want to print out messages from application to be different when it's in Debug mode and when it's in Release mode.  I'm confused about how to use the #if DEBUG  statement.  Do I need to have "#define DEBUG " statement at the top of the main page aspx?  Reading this link just confuses me, https://msdn.microsoft.com/en-us/library/4y6tbswk.aspx    and   http://stackoverflow.com/questions/1734741/how-to-execute-code-only-in-debug-mode-in-asp-net 

I want the program to auto detect if it's in DEBUG mode but if I use the define statement does that always put it in DEBUG mode?
Thank you.

Does syntax like this is correct?
#if DEBUG
     output this message;
#else
    output this other message;
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The DEBUG is by default set when you are compiling the debug configuration in VS however you can set it manually for any configuration

Visual studio build configuration
You do not have to set it manually. You just change your build configuration to Debug or ReleaseBuild configurationYour syntax is correct... Well mostly.... You always need a #endif... Whether you use #else or not.

#if DEBUG
     output this message;
#else
    output this other message;
#endif

Author

Commented:
Thank you.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
To your question :

Does syntax like this is correct?
#if DEBUG
     output this message;
#else
    output this other message;

If before this code you have #define DEBUG then "output this message;" will be executed if it is not appear before the statement then " output this other message;" this will be executed.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial