Contract.Ensures Method

Definition

Specifies a postcondition contract for the enclosing method or property.

Overloads

Ensures(Boolean)

Specifies a postcondition contract for the enclosing method or property.

Ensures(Boolean, String)

Specifies a postcondition contract for a provided exit condition and a message to display if the condition is false.

Ensures(Boolean)

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

Specifies a postcondition contract for the enclosing method or property.

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

Parameters

condition
Boolean

The conditional expression to test. The expression may include OldValue<T>(T), ValueAtReturn<T>(T), and Result<T>() values.

Attributes

Examples

The following example shows how to use the Ensures method to ensure that an expected value is returned. This code example is part of a larger example provided for the ContractClassAttribute class.

C#
int IArray.Add(Object value)
{
    // Returns the index in which an item was inserted.
    Contract.Ensures(Contract.Result<int>() >= -1);
    Contract.Ensures(Contract.Result<int>() < ((IArray)this).Count);
    return default(int);
}

Remarks

The condition parameter specifies a postcondition that is expected to be true when the enclosing method or property returns normally.

  • This method call must be at the beginning of a method or property, before any other code.

  • You must use the binary rewriter (available at Code Contracts on the Visual Studio Marketplace) for run-time enforcement of this postcondition.

Applies to

.NET 10 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, 10
.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

Ensures(Boolean, String)

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

Specifies a postcondition contract for a provided exit condition and a message to display if the condition is false.

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

Parameters

condition
Boolean

The conditional expression to test. The expression may include OldValue<T>(T) and Result<T>() values.

userMessage
String

The message to display if the expression is not true.

Attributes

Remarks

The condition parameter specifies a postcondition that is expected to be true when the enclosing method or property returns normally.

  • This method call must be at the beginning of a method or property, before any other code.

  • This contract is exposed to clients; therefore, it must only reference members that are at least as visible as the enclosing method.

  • You must use the binary rewriter (available at Code Contracts on the Visual Studio Marketplace) for run-time enforcement of this postcondition.

  • If userMessage is not a constant string literal, the contract may not be understood by tools.

Applies to

.NET 10 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, 10
.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