[c#] Will #if RELEASE work like #if DEBUG does in C#?

In all the examples I've seen of the #if compiler directive, they use "DEBUG". Can I use "RELEASE" in the same way to exclude code that I don't want to run when compiled in debug mode? The code I want to surround with this block sends out a bunch of emails, and I don't want to accidentally send those out when testing.

This question is related to c# .net debugging

The answer is


I've never seen that before...but I have seen:

#if (DEBUG == FALSE)

and

#if (!DEBUG)

That work for ya?


On my VS install (VS 2008) #if RELEASE does not work. However you could just use #if !DEBUG

Example:

#if !DEBUG
SendTediousEmail()
#endif

On my VS install (VS 2008) #if RELEASE does not work. However you could just use #if !DEBUG

Example:

#if !DEBUG
SendTediousEmail()
#endif

You can use #if(!DEBUG) for this purposes.


Nope.

While in debug configuration there is a DEBUG defined constant (automatically defined by Visual Studio) while there is no such constant defined for release mode. Check your project settings under build.

Selecting [Define DEBUG constant] under Project -> Build is like including #define DEBUG at the beginning of every file.

If you want to define a RELEASE constant for the release configuration go to:

  • Project Properties -> Build
  • Select Release Mode
  • in the Conditional compilation symbols textbox enter: RELEASE

You can create you own conditional compile-time symbols (any name you like). Go to the "project Build dialog", located in the project properties box, menu option: Project->[projectname] Properties...

You can also define them "at the top of the C# code file". Like:

#define RELEASE
// or
#undef RELEASE

you can use the symbol in a #if statement:

#if RELEASE
// code ...
#elif …
// code ...
#endif

// or

#if !RELEASE
// code ...
#endif

I know this is an old question, but it might be worth mentioning that you can create your own configurations outside of DEBUG and RELEASE, such as TEST or UAT.

If then on the Build tab of the project properties page you then set the "Conditional compilation symbols" to TEST (for instance) you can then use a construct such as

#if (DEBUG || TEST )
    //Code that will not be executed in RELEASE or UAT
#endif

You can use this construct for specific reason such as different clients if you have the need, or even entire Web Methods for instance. We have also used this in the past where some commands have caused issues on specific hardware, so we have a configuration for an app when deployed to hardware X.


I've never seen that before...but I have seen:

#if (DEBUG == FALSE)

and

#if (!DEBUG)

That work for ya?


Whilst M4N's answer (#if (!DEBUG)) makes most sense, another option could be to use the preprocessor to amend other flag's values; e.g.

bool isRelease = true;
#if DEBUG
    isRelease = false;
#endif

Or better, rather than referring to whether we're in release or debug mode, use flags that define the expected behavior and set them based on the mode:

bool sendEmails = true;
#if DEBUG
    sendEmails = false;
#endif

This is different to using preprocessor flags, in that the flags are still there in production, so you incur the overhead of if (sendEmails) {/* send mails */} each time that code's called, rather than the code existing in release but not existing in debug, but this can be advantageous; e.g. in your tests you may want to call your SendEmails() method but on a mock, whilst running in debug to get additional output.


Nope.

While in debug configuration there is a DEBUG defined constant (automatically defined by Visual Studio) while there is no such constant defined for release mode. Check your project settings under build.

Selecting [Define DEBUG constant] under Project -> Build is like including #define DEBUG at the beginning of every file.

If you want to define a RELEASE constant for the release configuration go to:

  • Project Properties -> Build
  • Select Release Mode
  • in the Conditional compilation symbols textbox enter: RELEASE

I know this is an old question, but it might be worth mentioning that you can create your own configurations outside of DEBUG and RELEASE, such as TEST or UAT.

If then on the Build tab of the project properties page you then set the "Conditional compilation symbols" to TEST (for instance) you can then use a construct such as

#if (DEBUG || TEST )
    //Code that will not be executed in RELEASE or UAT
#endif

You can use this construct for specific reason such as different clients if you have the need, or even entire Web Methods for instance. We have also used this in the past where some commands have caused issues on specific hardware, so we have a configuration for an app when deployed to hardware X.


"Pop Catalin" got it right. Controlling the definition based on the type of build provides a great deal of flexibility. For example, you can have a "DEBUG", "DEMO", and "RELEASE" configuration all in the same solution. That prevents the need for duplicate programming with two different solutions.

So yes #if RELEASE or #if (RELEASE) works the same as #if DEBUG when the RELEASE Conditional compilation symbol is defined.

The following is taken from "Pop Catalin" post: If you want to define a RELEASE constant for the release configuration go to: * Project Properties -> Build * Select Release Mode * in the Conditional compilation symbols textbox enter: RELEASE


RELEASE is not defined, but you can use

#if (!DEBUG)
  ...
#endif

Whilst M4N's answer (#if (!DEBUG)) makes most sense, another option could be to use the preprocessor to amend other flag's values; e.g.

bool isRelease = true;
#if DEBUG
    isRelease = false;
#endif

Or better, rather than referring to whether we're in release or debug mode, use flags that define the expected behavior and set them based on the mode:

bool sendEmails = true;
#if DEBUG
    sendEmails = false;
#endif

This is different to using preprocessor flags, in that the flags are still there in production, so you incur the overhead of if (sendEmails) {/* send mails */} each time that code's called, rather than the code existing in release but not existing in debug, but this can be advantageous; e.g. in your tests you may want to call your SendEmails() method but on a mock, whilst running in debug to get additional output.


why not just

#if RELEASE
#undef DEBUG
#endif

RELEASE is not defined, but you can use

#if (!DEBUG)
  ...
#endif

You can use #if(!DEBUG) for this purposes.


I've never seen that before...but I have seen:

#if (DEBUG == FALSE)

and

#if (!DEBUG)

That work for ya?


Nope.

While in debug configuration there is a DEBUG defined constant (automatically defined by Visual Studio) while there is no such constant defined for release mode. Check your project settings under build.

Selecting [Define DEBUG constant] under Project -> Build is like including #define DEBUG at the beginning of every file.

If you want to define a RELEASE constant for the release configuration go to:

  • Project Properties -> Build
  • Select Release Mode
  • in the Conditional compilation symbols textbox enter: RELEASE

On my VS install (VS 2008) #if RELEASE does not work. However you could just use #if !DEBUG

Example:

#if !DEBUG
SendTediousEmail()
#endif

You can create you own conditional compile-time symbols (any name you like). Go to the "project Build dialog", located in the project properties box, menu option: Project->[projectname] Properties...

You can also define them "at the top of the C# code file". Like:

#define RELEASE
// or
#undef RELEASE

you can use the symbol in a #if statement:

#if RELEASE
// code ...
#elif …
// code ...
#endif

// or

#if !RELEASE
// code ...
#endif

I've never seen that before...but I have seen:

#if (DEBUG == FALSE)

and

#if (!DEBUG)

That work for ya?


RELEASE is not defined, but you can use

#if (!DEBUG)
  ...
#endif

why not just

#if RELEASE
#undef DEBUG
#endif

On my VS install (VS 2008) #if RELEASE does not work. However you could just use #if !DEBUG

Example:

#if !DEBUG
SendTediousEmail()
#endif

"Pop Catalin" got it right. Controlling the definition based on the type of build provides a great deal of flexibility. For example, you can have a "DEBUG", "DEMO", and "RELEASE" configuration all in the same solution. That prevents the need for duplicate programming with two different solutions.

So yes #if RELEASE or #if (RELEASE) works the same as #if DEBUG when the RELEASE Conditional compilation symbol is defined.

The following is taken from "Pop Catalin" post: If you want to define a RELEASE constant for the release configuration go to: * Project Properties -> Build * Select Release Mode * in the Conditional compilation symbols textbox enter: RELEASE


why not just

#if RELEASE
#undef DEBUG
#endif

RELEASE is not defined, but you can use

#if (!DEBUG)
  ...
#endif

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to debugging

How do I enable logging for Spring Security? How to run or debug php on Visual Studio Code (VSCode) How do you debug React Native? How do I debug "Error: spawn ENOENT" on node.js? How can I inspect the file system of a failed `docker build`? Swift: print() vs println() vs NSLog() JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..." How to debug Spring Boot application with Eclipse? Unfortunately MyApp has stopped. How can I solve this? 500 internal server error, how to debug