Επεξεργασία

Κοινή χρήση μέσω


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.

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

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.

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);
}
Function Add(ByVal value As Object) As Integer Implements IArray.Add
    ' Returns the index in which an item was inserted.
    Contract.Ensures(Contract.Result(Of Integer)() >= -1) '
    Contract.Ensures(Contract.Result(Of Integer)() < CType(Me, IArray).Count) '
    Return 0
    
End Function 'IArray.Add

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

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.

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

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