If I define the Debug constant for my C# Project in visual studio I can be sure that assertions will be evaluated and a messagebox is shown when they fail. But what flag, attribute makes the CLR at runtime actually decide whether a an assertion is evaluated and displayed. Does the assertion code not end up in the IL when DEBUG is defined? Or is it the DebuggableAttribute.DebuggingModes flag in the DebuggableAttribute of the assembly the crucial point? If so, what enum value of it must be present? How does this work under the hood?
Share
If you compile without the DEBUG preprocessor symbol defined, any calls to Debug.Assert will be omited from the compiled code.
If you look at the docs for Debug.Assert you’ll see it has
[ConditionalAttribute('DEBUG')]on the declaration. ConditionalAttribute is used to decide whether or not a method call is actually emitted or not at compile-time.If a conditional attribute means that the call isn’t made, any argument evaluation is also left out. Here’s an example:
When TEST is defined, both methods are called:
When TEST isn’t defined, neither is called: