Reputation: 18117
I have code which I would like to enable for compilation when I build project using Release configuration and disable while debugging. How to do what?
Upvotes: 2
Views: 6681
Reputation: 23142
Use a preprocessor directive. Surround the code with:
#if !DEBUG
// Release-only code goes here...
#endif
In the standard debug and release configurations in Visual Studio, the DEBUG
symbol is defined when compiling in debug and not in release, so code in between the two directives above will only be compiled in release mode.
If you need to do one thing in debug and another thing in release, you can do this:
#if DEBUG
// Debug-only code goes here...
#else
// Release-only code goes here...
#endif
See the C# preprocessor documentation for more details.
Upvotes: 3
Reputation: 499212
Use a preprocessor directive.
#IF ! DEBUG
//Your code here
#ENDIF
Though, if your code is full of these, you may want to consider other options, such as
Another alternative is to use the ConditionalAttribute
on a method so it will only be used if a specific symbol has been defined:
[Conditional("RELEASE")]
public void MyReleaseMethod()
{
}
Upvotes: 9