Monday 22 January 2007

Conditional("Debug")

There's a pretty cool feature in C#(or MSIL compiler?). On a method, you can put a conditional attribute, and calls to that method are only compiled on debug mode. This gives a perfect way to trace how are things going on.

public class Test{
[Conditional("Debug")]
public void Method()
{
// ....
}
}


Today, I've found a bug in my code, associated with this.
[Conditional("DEBUG")]

I had written the string in capital letters. But in runtime or compile-time, there's not an error about it. It simply and silently "doesn't work."

It took an hour to realize this. It would be great if they used an enum, for examle:
[Conditional(ConditionalModes.Debug)]

or different attribute types for each mode:
[DebugConditional]

This is not a big thing of course, but these kind of design decisions are important.
I always prefer compile time errors -where possible- :)

No comments: