Contract.Assert Method

Definition

In debug builds, performs a run-time test for a specified condition.

Overloads

Assert(Boolean)

Checks for a condition; if the condition is false, follows the escalation policy set for the analyzer.

Assert(Boolean, String)

Checks for a condition; if the condition is false, follows the escalation policy set by the analyzer and displays the specified message.

Assert(Boolean)

Source:
Contracts.cs
Source:
Contracts.cs
Source:
Contracts.cs

Checks for a condition; if the condition is false, follows the escalation policy set for the analyzer.

C#
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
[System.Diagnostics.Conditional("DEBUG")]
public static void Assert(bool condition);

Parameters

condition
Boolean

The conditional expression to test.

Attributes

Remarks

The default escalation policy is to notify the attached debugger about a contract failure or to display an Assert dialog box if a debugger is not attached. Optionally, the analyzer can be instructed to throw an exception.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Assert(Boolean, String)

Source:
Contracts.cs
Source:
Contracts.cs
Source:
Contracts.cs

Checks for a condition; if the condition is false, follows the escalation policy set by the analyzer and displays the specified message.

C#
[System.Diagnostics.Conditional("DEBUG")]
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public static void Assert(bool condition, string userMessage);
C#
[System.Diagnostics.Conditional("DEBUG")]
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public static void Assert(bool condition, string? userMessage);

Parameters

condition
Boolean

The conditional expression to test.

userMessage
String

A message to display if the condition is not met.

Attributes

Remarks

The default escalation policy is to notify the attached debugger about a contract failure or to display an Assert dialog box if a debugger is not attached. Optionally, the analyzer can be instructed to throw an exception. The userMessage parameter is passed to the debugger output. If userMessage is not a constant string literal, tools may not be able to read it.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0