Rediger

Del via


Debug.Assert Method

Definition

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

Overloads

Assert(Boolean)

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

Assert(Boolean, Debug+AssertInterpolatedStringHandler)

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

Assert(Boolean, String)

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

Assert(Boolean, Debug+AssertInterpolatedStringHandler, Debug+AssertInterpolatedStringHandler)

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

Assert(Boolean, String, String)

Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack.

Assert(Boolean, String, String, Object[])

Checks for a condition; if the condition is false, outputs two messages (simple and formatted) and displays a message box that shows the call stack.

Assert(Boolean)

Source:
Debug.cs
Source:
Debug.cs
Source:
Debug.cs

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

public:
 static void Assert(bool condition);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition);
[<System.Diagnostics.Conditional("DEBUG")>]
static member Assert : bool -> unit
Public Shared Sub Assert (condition As Boolean)

Parameters

condition
Boolean

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

Attributes

Examples

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
// Create an index for an array.
int index;

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

// Test that the index value is valid.
Debug.Assert(index > -1);
' Create an index for an array.
Dim index As Integer

' Perform some action that sets the index.
index = -40

' Test that the index value is valid. 
Debug.Assert((index > - 1))

Remarks

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.

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.

Note

Windows 8.x 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 DefaultTraceListener. If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. The DefaultTraceListener can be removed by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()). For .NET Framework apps, you can also use the <clear> element and the <remove> element in your app's configuration file.

For .NET Framework apps, 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>

See also

Applies to

Assert(Boolean, Debug+AssertInterpolatedStringHandler)

Source:
Debug.cs
Source:
Debug.cs
Source:
Debug.cs

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

public:
 static void Assert(bool condition, System::Diagnostics::Debug::AssertInterpolatedStringHandler % message);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, ref System.Diagnostics.Debug.AssertInterpolatedStringHandler message);
[<System.Diagnostics.Conditional("DEBUG")>]
static member Assert : bool * AssertInterpolatedStringHandler -> unit
Public Shared Sub Assert (condition As Boolean, ByRef message As Debug.AssertInterpolatedStringHandler)

Parameters

condition
Boolean

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

message
Debug.AssertInterpolatedStringHandler

The message to send to the Listeners collection.

Attributes

Remarks

This overload was introduced in .NET 6 to improve performance. In comparison to the overloads that take a String parameter, this overload only evaluates any interpolated string formatting items if the message is required.

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.

Typically, the Assert method is used to identify logic errors during program development. Assert evaluates the condition. If the result is false, it sends the specified diagnostic 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.

Note

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 calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()). For .NET Framework apps, you can also use the <clear> element and the <remove> element in your app's configuration file.

For .NET Framework apps, 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>

See also

Applies to

Assert(Boolean, String)

Source:
Debug.cs
Source:
Debug.cs
Source:
Debug.cs

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

public:
 static void Assert(bool condition, System::String ^ message);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string message);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string? message);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string? message = default);
[<System.Diagnostics.Conditional("DEBUG")>]
static member Assert : bool * string -> unit
Public Shared Sub Assert (condition As Boolean, message As String)
Public Shared Sub Assert (condition As Boolean, Optional message As String = Nothing)

Parameters

condition
Boolean

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

message
String

The message to send to the Listeners collection.

Attributes

Examples

The following example checks whether the type parameter is valid. If type is null, Assert outputs a message.

void MyMethod( Object^ obj, Type^ type )
{
   #if defined(DEBUG)
   Debug::Assert( type != nullptr, "Type paramater is null" );
   #endif
}
public static void MyMethod(Type type, Type baseType)
{
    Debug.Assert(type != null, "Type parameter is null");

    // Perform some processing.
}
Public Shared Sub MyMethod(type As Type, baseType As Type)
    Debug.Assert(Not (type Is Nothing), "Type parameter is null")
End Sub

Remarks

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.

Typically, the Assert method is used to identify logic errors during program development. Assert evaluates the condition. If the result is false, it sends the specified diagnostic 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.

Note

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 calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()). For .NET Framework apps, you can also use the <clear> element and the <remove> element in your app's configuration file.

For .NET Framework apps, 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>

See also

Applies to

Assert(Boolean, Debug+AssertInterpolatedStringHandler, Debug+AssertInterpolatedStringHandler)

Source:
Debug.cs
Source:
Debug.cs
Source:
Debug.cs

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

public:
 static void Assert(bool condition, System::Diagnostics::Debug::AssertInterpolatedStringHandler % message, System::Diagnostics::Debug::AssertInterpolatedStringHandler % detailMessage);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, ref System.Diagnostics.Debug.AssertInterpolatedStringHandler message, ref System.Diagnostics.Debug.AssertInterpolatedStringHandler detailMessage);
[<System.Diagnostics.Conditional("DEBUG")>]
static member Assert : bool * AssertInterpolatedStringHandler * AssertInterpolatedStringHandler -> unit
Public Shared Sub Assert (condition As Boolean, ByRef message As Debug.AssertInterpolatedStringHandler, ByRef detailMessage As Debug.AssertInterpolatedStringHandler)

Parameters

condition
Boolean

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

message
Debug.AssertInterpolatedStringHandler

The message to send to the Listeners collection.

detailMessage
Debug.AssertInterpolatedStringHandler

The detailed message to send to the Listeners collection.

Attributes

Remarks

This overload was introduced in .NET 6 to improve performance. In comparison to the overloads that take a String parameter, this overload only evaluates any interpolated string formatting items if the message is required.

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.

Typically, the Assert method is used to identify logic errors during program development. Assert evaluates the condition. If the result is false, it sends the specified diagnostic 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.

Note

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 calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()). For .NET Framework apps, you can also use the <clear> element and the <remove> element in your app's configuration file.

For .NET Framework apps, 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>

See also

Applies to

Assert(Boolean, String, String)

Source:
Debug.cs
Source:
Debug.cs
Source:
Debug.cs

Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack.

public:
 static void Assert(bool condition, System::String ^ message, System::String ^ detailMessage);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string message, string detailMessage);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string? message, string? detailMessage);
[<System.Diagnostics.Conditional("DEBUG")>]
static member Assert : bool * string * string -> unit
Public Shared Sub Assert (condition As Boolean, message As String, detailMessage As String)

Parameters

condition
Boolean

The conditional expression to evaluate. If the condition is true, the specified messages are not sent and the message box is not displayed.

message
String

The message to send to the Listeners collection.

detailMessage
String

The detailed message to send to the Listeners collection.

Attributes

Examples

The following example checks whether the type parameter is valid. If type is null, Assert outputs two messages.

void MyMethod( Object^ obj, Type^ type )
{
   #if defined(DEBUG)
   Debug::Assert( type != nullptr, "Type paramater is null", "Can't get object for null type" );
   #endif
}
public static void MyMethod(Type type, Type baseType)
{
    Debug.Assert(type != null, "Type parameter is null",
       "Can't get object for null type");

    // Perform some processing.
}
Public Shared Sub MyMethod(type As Type, baseType As Type)
    Debug.Assert( Not (type Is Nothing), "Type parameter is null", "Can't get object for null type")
    ' Perform some processing.
End Sub

Remarks

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.

Typically, the Assert(Boolean, String, String) method is used to identify logic errors during program development. Assert evaluates the condition. If the result is false, it sends the specified diagnostic message and detailed 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.

Note

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 calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()). For .NET Framework apps, you can also use the <clear> element and the <remove> element in your app's configuration file.

For .NET Framework apps, 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>

See also

Applies to

Assert(Boolean, String, String, Object[])

Source:
Debug.cs
Source:
Debug.cs
Source:
Debug.cs

Checks for a condition; if the condition is false, outputs two messages (simple and formatted) and displays a message box that shows the call stack.

public:
 static void Assert(bool condition, System::String ^ message, System::String ^ detailMessageFormat, ... cli::array <System::Object ^> ^ args);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string message, string detailMessageFormat, params object[] args);
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert (bool condition, string? message, string detailMessageFormat, params object?[] args);
[<System.Diagnostics.Conditional("DEBUG")>]
static member Assert : bool * string * string * obj[] -> unit
Public Shared Sub Assert (condition As Boolean, message As String, detailMessageFormat As String, ParamArray args As Object())

Parameters

condition
Boolean

The conditional expression to evaluate. If the condition is true, the specified messages are not sent and the message box is not displayed.

message
String

The message to send to the Listeners collection.

detailMessageFormat
String

The composite format string to send to the Listeners collection. This message contains text intermixed with zero or more format items, which correspond to objects in the args array.

args
Object[]

An object array that contains zero or more objects to format.

Attributes

Remarks

This method uses the .NET composite formatting feature to convert the value of an object to its text representation and embed that representation in a string. The resulting string is sent to the Listeners collection.

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.

Typically, the Assert(Boolean, String, String, Object[]) method is used to identify logic errors during program development. Assert evaluates the condition. If the result is false, The String.Format(String, Object[]) method is called and the detailMessageFormat string and args array are passed in as parameters. Assert(Boolean, String, String, Object[]) then sends the specified text message and the formatted text 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.

Note

The display of the message box is dependent 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 calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()). For .NET Framework apps, you can also use the <clear> element and the <remove> element in your app's configuration file.

For .NET Framework apps, 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>

Applies to