Removing code from Release build in .NET

You can apply the ConditionalAttribute attribute, with the string "DEBUG" to any method and calls to that item will only be present in DEBUG builds.

This differs from using the #ifdef approach as this allows you to release methods for use by other people in their DEBUG configurations (like the Debug class methods in the .NET framework).


Have a look at preprocessor directives...

#if DEBUG
    //code
#endif

Visual Studio defines a DEBUG constant for the Debug configuration and you can use this to wrap the code that you don't want executing in your Release build:

#ifdef DEBUG
  // Your code
#endif

However, you can also decorate a method with a Conditional attribute, meaning that the method will never be called for non-Debug builds (the method and any call-sites will be removed from the assembly):

[Conditional("DEBUG")]
private void MyDebugMethod()
{
  // Your code
}

Tags:

C#

Debugging