Search code examples
c#assertions

Why is this Debug.Assert skipped?


I have the following code:

Debug.Assert(model.OrganizationId != null, "model.OrganizationId != null");
var orgId = model.OrganizationId.Value;

Yet the Debug.Assert line is grayed out, and when I hover over it, I get the message:

Method invocation is skipped. Compiler will not generate method invocation because method is conditional, or is partial method without implementation.

My IDE is in Debug mode and I see nothing else unusual. Why is this assert being skipped? I am not too concerned with OrganizationId being null, as it is marked as Required on the model, but I am concerned that a very normal looking Debug.Assert is being skipped.


Solution

  • Debug.Assert is marked with this attribute.

    [System.Diagnostics.Conditional("DEBUG")]
    

    That means this method is compiled when you have the "DEBUG" constant defined in your project configuration.

    Note that this is irrelevant to release or debug build mode. But by default debug configuration has "DEBUG" constant defined.

    In order to fix this goto your project settings, build section, make sure configuration is set to Debug. Then tick the option "define DEBUG constant".