In C#, is a Debug.Assert test run in release mode?

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?

Answers


What happens to the the Debug.Assert method when I compile in release mode?

It's completely removed (including the call to ExpensiveTest), assuming you don't have the DEBUG conditional compilation symbol defined in your release configuration.

If you look at the documentation, the declaration uses [ConditionalAttribute("DEBUG")]:

[ConditionalAttribute("DEBUG")]
public static void Assert(
    bool condition
)

ConditionalAttribute is 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.


Assertions in Managed Code - MSDN

In Visual Basic and Visual C#, you can use the Assert method from either Debug or Trace, which are in the System.Diagnostics namespace. Debug class methods are not included in a Release version of your program, so they do not increase the size or reduce the speed of your release code.

Also from the same link:

Note that calls to the Debug.Assert method disappear when you create a Release version of your code. That means that the call that checks the balance disappears in the Release version. To solve this problem, you should replace Debug.Assert with Trace.Assert, which does not disappear in the Release version


According to Debug.Assert Method (Boolean) Debug methods are compiled only in debug builds.

So, it you build correct release build (see menu item Debug/Configuration Manager for details) this method call will be removed.


Q. In C#, is a Debug.Assert test run in release mode?

The answer is "No." From Microsoft support: How to trace and debug in Visual C#:

You can use the Trace and the Debug classes separately or together in the same application. In a Debug Solution Configuration project, both Trace and Debug output are active. The project generates output from both of these classes to all Listener objects. However, a Release Solution Configuration project only generates output from a Trace class. The Release Solution Configuration project ignores any Debug class method invocations.

In particular, the last sentence makes it clear that Debug.Assert() statements (as well as other Debug class method invocations) are ignored in a Release build.


Need Your Help

How to use Android ViewSwitcher?

android android-mapview viewswitcher

I'm trying to use the ViewSwitcher to perform switches between two views. One is an ImageView and the other is a MapView. I have a button which the user clicks to perform this switch but my codes are

Sending mail using SmtpClient in .net

c# .net asp.net smtpclient

I am unable to send the mail using smtp client.