Debug.Assert Method (Boolean)
Assembly: System (in system.dll)
/** @attribute ConditionalAttribute("DEBUG") */ public static void Assert ( boolean condition )
true to prevent a message being displayed; otherwise, false.
Assert outputs the call stack with file and line numbers for each line in the call stack.
Typically, Assert is used to identify logic errors during program development. Assert evaluates the condition. If the result is false, it sends diagnostic messages to the Listeners.
The default behavior displays a message box when the application runs in user-interface mode, and outputs the message to the default trace output. You can customize this behavior by adding a TraceListener to, or removing one from, the Listeners collection.
To set an assert, you can also edit the configuration file corresponding to the name of your application. Within this file, you can enable and disable the assert or set the name of its log file. The configuration file should be formatted like the following example:
<configuration> <system.diagnostics> <assert assertuienabled="true" logfilename="c:\\myFile.log" /> </system.diagnostics> </configuration>
The following example creates an index for an array. Then some action is performed that sets the value of the index. Next the code calls Assert to confirm the index value is valid. If it is not valid, Assert outputs the call stack.
// Create an index for an array. int index; // Perform some action that sets the index. // Test that the index value is valid. Debug.Assert(index > -1);
// Create an index for an array. int index = 0; // Test that the index value is valid. Debug.Assert(index > -1);
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.