Debug.Assert Method (Boolean)
Checks for a condition; if the condition is false, displays a message box that shows the call stack.
Assembly: System (in System.dll)
The Assert methods are not available for Windows Store apps.
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.
Windows 8.x 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.
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 callsto confirm that the index value is valid. If it is not valid, outputs the call stack.
Available since 8
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 8.1