stighy
stighy

Reputation: 7170

How to tell "if on development environment"?

I'd like to add a condition to my C# program i'm develop in Visual Studio:

#If onEditor 
   do something

In Unity exists if Application.isEditor

Does exists something in 'regular' (WinForms or ASP.NET) C# application on Visual Studio?

Upvotes: 3

Views: 7248

Answers (4)

Hossein Ebrahimi
Hossein Ebrahimi

Reputation: 822

The full syntax for the preprocessor directive is:

#if DEBUG
// Do something
#endif

Your code will only run in a debug build this way, whether or not the debugger is attached.

Upvotes: 1

Jeremy Thompson
Jeremy Thompson

Reputation: 65594

Use this in Visual Studio to test if you're running in Debug mode:

if (Debugger.IsAttached)
{
   Debugger.Break();
}

Use #if DEBUG to conditionally compile code in - either debug mode or release mode.

Upvotes: 4

Patrick Hofman
Patrick Hofman

Reputation: 156978

#if are compile directives, so whatever goes there will be checked on compile time, and not on runtime. There is #if DEBUG which effectively means 'this was build in debug mode', rather than 'release mode'. It doesn't tell anything about the origin of the running if your program.

I think what you are looking for is Debugger.IsAttached: it checks if a debugger is attached. If that it true, the program is either ran from Visual Studio, or a debugger was attached later on.

Upvotes: 10

Martin Zikmund
Martin Zikmund

Reputation: 39082

You can use:

#if DEBUG

DEBUG constant is defined for Debug configuration in all default Visual Studio project templates.

Upvotes: 3

Related Questions