'How to check if a app is in debug or release

I am busy making some optimizations to a app of mine, what is the cleanest way to check if the app is in DEBUG or RELEASE



Solution 1:[1]

At compile time or runtime? At compile time, you can use #if DEBUG. At runtime, you can use [Conditional("DEBUG")] to indicate methods that should only be called in debug builds, but whether this will be useful depends on the kind of changes you want to make between debug and release builds.

Solution 2:[2]

static class Program
{
    public static bool IsDebugRelease
    {
        get
        {
 #if DEBUG
            return true;
 #else
            return false;
 #endif
        }
     }
 }

Though, I tend to agree with itowlson.

Solution 3:[3]

Personally I don't like the way #if DEBUG changes the layout. I do it by creating a conditional method that is only called when in debug mode and pass a boolean by reference.

[Conditional("DEBUG")]
private void IsDebugCheck(ref bool isDebug)
{
    isDebug = true;
}
 
public void SomeCallingMethod()
{ 
    bool isDebug = false;
    IsDebugCheck(ref isDebug);

    //do whatever with isDebug now
}

Solution 4:[4]

I tend to put something like the following in AssemblyInfo.cs:

#if DEBUG
[assembly: AssemblyConfiguration("Debug build")]
#else
[assembly: AssemblyConfiguration("Release build")]
#endif

Solution 5:[5]

You can use ILSpy both for exe and for dll. Just drag your DLL\EXE to the explorer sidebar and you can see at: [assembly: Debuggable line ....

Example 1: Compile as Release mode: enter image description here

Example 2: Compile as Debug mode: enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 itowlson
Solution 2 Matthew Scharley
Solution 3 phoenix
Solution 4 Joe
Solution 5 Yohan