Tomas
Tomas

Reputation: 18117

Enable code on Release compilation

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

Answers (2)

FishBasketGordo
FishBasketGordo

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

Oded
Oded

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

Related Questions