Take the following example:
public void Foo()
{
//Code...
Debug.Assert(ExpensiveTest());
//Code...
}
What happens to the the Debug.Assert method when I compile in release mode? Would ExpensiveTest() still run? If not, then how does it work (since it is not a macro that can be set to evaluate to nothing)? If it does run, then doesn’t that defeat the purpose of debug assertions?
It’s completely removed (including the call to
ExpensiveTest), assuming you don’t have theDEBUGconditional compilation symbol defined in your release configuration.If you look at the documentation, the declaration uses
[ConditionalAttribute("DEBUG")]:ConditionalAttributeis used for conditional compilation. See Bart de Smet’s blog post on conditional compilation for more details, along with section 17.4.2 of the C# 4 specification.