class Debug
{
internal static void Assert(bool condition)
{
#if DEBUG
Log.Out("Asserted");
#endif
}
}
Will the compiler get rid of calling Assert, as it’s empty in Release builds and Optimize checkbox is checked, or there will be a calling empty method overhead?
Regards,
No, the C# compiler won’t remove the call to
Assert– it’ll just be an empty method. The JIT compiler may optimize it away in calling code; effectively that’s a special case of inlining, where the result of inlining is “nothing to execute”. However, note that the argument toAssertwill still be evaluated.However, if you want to make the call itself conditional, a cleaner approach is to change the method to use the
System.Diagnostics.Conditionalattribute:This changes the semantics: now the whole method call including argument evaluation will be removed by the C# compiler, so you could have:
and in debug mode it would hit the database, but in release mode it wouldn’t.