ArekBee
ArekBee

Reputation: 311

ConditionalAttribute vs Debugger.IsAttached

What is the difference between ConditionalAttribute and Debugger.IsAttached?? Which of this mechanism is better to use??

If I write:

[Conditional("DEBUG")]
private void Method() 
{ 
   //Code
} 

will be the same like:

private void Method()
{ 
    if (Debugger.IsAttached)
    { 
        //Code
    } 
}

Upvotes: 1

Views: 217

Answers (1)

Anders Forsgren
Anders Forsgren

Reputation: 11101

The attribute works for compilation. If the DEBUG flag is not set the method is omitted from the produced binary.

The Debugger is attached checks if a debugger is attached. But a debugger can be attached to any kind of build (Release, Debug etc.)

Since they are quite different, I don't think they can be compared for which one is "better". They do different things. If you want something for a debug build that should have no effect (e.g. performance) on a production build at all, then use the attribute.

Upvotes: 4

Related Questions