Export (0) Print
Expand All

Trace.Assert Method (Boolean)

Checks for a condition, and outputs the call stack if the condition is false.

Namespace: System.Diagnostics
Assembly: System (in system.dll)

[ConditionalAttribute("TRACE")] 
public static void Assert (
	bool condition
)
/** @attribute ConditionalAttribute("TRACE") */ 
public static void Assert (
	boolean condition
)
ConditionalAttribute("TRACE") 
public static function Assert (
	condition : boolean
)

Parameters

condition

true to prevent a message being displayed; otherwise, false.

Assert outputs the call stack with file and line number for each line in the call stack.

Typically, you use Assert to identify logic errors during program development. Assert will evaluate the condition. If the result is false, it sends diagnostic messages to the Assert.

The default behavior displays a message box when the application runs in user-interface mode, and to output 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 that corresponds 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>
    <switches>
      <add name="mySwitch" value="4"/>
    </switches>
    <trace autoflush="false" indentsize="4"/>
    <assert assertuienabled="true" logfilename=".\TraceLog.txt"/>
  </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 verify the index value is valid. If it is not valid, the Assert outputs the call stack.

// Create an index for an array.
 protected int index;
 
 protected void Method()
 { 
 // Perform some action that sets the index.
 
 // Test that the index value is valid. 
 Trace.Assert(index > -1);
 }  

// Create an index for an array.
protected int index;
protected void Method()
{
    // Perform some action that sets the index.
    // Test that the index value is valid. 
    Trace.Assert((index > -1));
} //Method

import System
import System.Diagnostics
    
var array : int[] = [4, 3, 2, 1]

for (var index : int = 0; index <= array.Length; index++){
  // Test that the index value is valid. 
  Trace.Assert((0 <= index) && (index < array.Length))
  print(array[index])
}


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.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft