unarity
unarity

Reputation: 2445

Windows 8 app is debug

Is there a way to checkin app.cs is curent state of app debug or deployment?

Or the problem is that I want to exceute piece of code only when debuging application.

Upvotes: 0

Views: 611

Answers (3)

Patrick Klug
Patrick Klug

Reputation: 14381

You can simply use the #if DEBUG directive like so

#if DEBUG
    //code here only executes in debug
#endif

So if you want some code that runs in DEBUG and some that is in RELEASE you do it like this:

#if DEBUG
    //code here only executes in debug
#else
    //code here only executes in release
#endif

And as DAKL has explained you could also use the Conditional attribute.

Upvotes: 5

Jared Bienz - MSFT
Jared Bienz - MSFT

Reputation: 3550

The other answers tell you how to check at runtime if your app is compiled as a Debug build. If you're wanting to see if Visual Studio (or any other debugger) is attached and debugging your app you can use System.Diagnostics.Debugger.IsAttached.

Upvotes: 1

DAKL
DAKL

Reputation: 39

You can use [ConditionalAttribute("DEBUG")] for that.

If you want a method only to be executed in debug mode you can do the following:

[ConditionalAttribute("DEBUG")]
public void WriteOnlyInDebug(string message)
{
    Console.WriteLine(message);
}

This method is only called in Debug mode. The method and all calls to it are getting removed from the binary when you build your app in release mode.

Upvotes: 3

Related Questions