System.Diagnostics.Debug.Assert Method

Checks for a condition; if the condition is false, displays a message box that shows the call stack.

Syntax

[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition)

Parameters

condition
The conditional expression to evaluate. If the condition is true, a failure message is not sent and the message box is not displayed.

Remarks

By default, the erload:System.Diagnostics.Debug.Assert method works only in debug builds. Use the erload:System.Diagnostics.Trace.Assert method if you want to do assertions in release builds. For more information, see Assertions in Managed Code.

Note:

The Assert methods are not available for win8_appstore_long apps.

Typically, the Debug.Assert(bool) method is used to identify logic errors during program development. Debug.Assert(bool) evaluates the condition. If the result is false, it sends a failure message to the Debug.Listeners collection. You can customize this behavior by adding a System.Diagnostics.TraceListener to, or removing one from, the Debug.Listeners collection.

When the application runs in user interface mode, it displays a message box that shows the call stack with file and line numbers. The message box contains three buttons: Abort, Retry, and Ignore. Clicking the Abort button terminates the application. Clicking Retry sends you to the code in the debugger if your application is running in a debugger, or offers to open a debugger if it is not. Clicking Ignore continues with the next instruction in the code.

Note:

win8_appname_long apps do not support modal dialog boxes, so they behave the same in user interface mode and non-user interface mode. The message is written to the active trace listeners in debugging mode, or no message is written in release mode.

Note:

The display of the message box depends on the presence of the System.Diagnostics.DefaultTraceListener. If the System.Diagnostics.DefaultTraceListener is not in the Trace.Listeners collection, the message box is not displayed. The System.Diagnostics.DefaultTraceListener can be removed by the <clear> Element for <listeners> for <trace>, the <remove> Element for <listeners> for <trace>, or by calling the TraceListenerCollection.Clear method on the Trace.Listeners property (System.Diagnostics.Trace.Listeners.Clear()).

You can change the behavior of the System.Diagnostics.DefaultTraceListener in the configuration file that corresponds to the name of your application. In this file, you can enable and disable the assert message box or set the DefaultTraceListener.LogFileName property. The configuration file should be formatted as follows:

Example

<configuration>
  <system.diagnostics>
    <assert assertuienabled="true" logfilename="c:\\myFile.log" />
  </system.diagnostics>
</configuration>

Requirements

Namespace: System.Diagnostics
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0