Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Debug.Assert/Debug.Fail automatically conditionally compiled #if "DEBUG"

Tags:

c#

.net

debugging

Are Debug.Assert/Debug.Fail automatically conditionally compiled #if "DEBUG"? Or is it more like if there is no debugger attached (even in release) it just doesn't do much of anything? If so, are there performance implications of leaving them in your code? Or are they really not meant to be in production code, only test or Conditional code?

like image 360
Professional Sounding Name Avatar asked Mar 07 '09 23:03

Professional Sounding Name


People also ask

How does Debug Assert work?

Assert(Boolean, Debug+AssertInterpolatedStringHandler, Debug+AssertInterpolatedStringHandler) Checks for a condition; if the condition is false , outputs a specified message and displays a message box that shows the call stack.

Does debug assert work in Release mode?

Assert works only in DEBUG mode - or, at least, when the DEBUG variable is defined. Otherwise, all those checks will simply get removed from the build result, so they will not impact your application when running in RELEASE mode.

Why use Debug Assert?

Assert is typically used when debugging to test an expression that should evaluate to True. If it doesn't, the Immediate window can be used to investigate why the test failed.

Can asserts help with debugging?

Debugging Your Code With Assertions. At its core, the assert statement is a debugging aid for testing conditions that should remain true during your code's normal execution. For assertions to work as a debugging tool, you should write them so that a failure indicates a bug in your code.


2 Answers

No, the whole call, incuding any expression evaluation is removed from compilation if the symbol isn't defined. This is very important - if there are any side-effects in the expression, they won't occur if DEBUG isn't defined. Here's a short but complete program to demonstrate:

using System;
using System.Diagnostics;

class Test
{
    static void Main()
    {
        int i = 0;
        Debug.Assert(i++ < 10);
        Console.WriteLine(i);
    }
}

If DEBUG is defined this prints out 1, otherwise it prints 0.

Due to this kind of behaviour, you can't have an out parameter on a conditionally-compiled method:

using System;
using System.Diagnostics;

class Test
{
    static void Main()
    {
        int i ;
        MethodWithOut(out x);
    }

    [Conditional("FOO")]
    static void MethodWithOut(out int x)
    {
        x = 10;
    }
}

This gives the error:

Test.cs(13,6): error CS0685: Conditional member 'Test.MethodWithOut(out int)' cannot have an out parameter

like image 116
Jon Skeet Avatar answered Oct 24 '22 18:10

Jon Skeet


The Debug.Assert/Fail API's contain a ConditionalAttribute attribute with the value "DEBUG" like so

[Conditional("DEBUG")]
public void Assert(bool condition)

The C# and VB compiler will only actually include a call to the is method if the constant DEBUG is defined when the method call is compiled in code. If it's no there, the method call will be ommitted from the IL

like image 22
JaredPar Avatar answered Oct 24 '22 17:10

JaredPar