Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a app is in debug or release

Tags:

c#

.net

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

like image 667
RC1140 Avatar asked Oct 23 '09 04:10

RC1140


3 Answers

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

Though, I tend to agree with itowlson.

like image 99
Matthew Scharley Avatar answered Sep 20 '22 00:09

Matthew Scharley


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
}
like image 28
ShrapNull Avatar answered Sep 24 '22 00:09

ShrapNull


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.

like image 39
itowlson Avatar answered Sep 22 '22 00:09

itowlson