Export (0) Print
Expand All

Debug::Assert Method (Boolean)

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

Namespace:  System.Diagnostics
Assemblies:   System.Diagnostics.Debug (in System.Diagnostics.Debug.dll)
  System (in System.dll)

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

Parameters

condition
Type: System::Boolean

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

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

Important noteImportant

The Assert methods are not available for Windows Store apps.

Typically, the Assert(Boolean) method is used to identify logic errors during program development. Assert evaluates the condition. If the result is false, it sends a failure message to the Listeners collection. You can customize this behavior by adding a TraceListener to, or removing one from, the 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.

NoteNote

Windows Store 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.

NoteNote

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

You can change the behavior of the 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:

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

The following example creates an index for an array, performs some action to set the value of the index, and then calls Assert to confirm that the index value is valid. If it is not valid, Assert outputs the call stack.

// Create a local value. 
int index;

// Perform some action that sets the local value.
index = -40;

// Test that the local value is valid. 
#if defined(DEBUG)
Debug::Assert( index > -1 );
#endif

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft