Contract.Ensures 方法

定義

指定封入方法或屬性的後置條件合約。Specifies a postcondition contract for the enclosing method or property.

多載

Ensures(Boolean)

指定封入方法或屬性的後置條件合約。Specifies a postcondition contract for the enclosing method or property.

Ensures(Boolean, String)

指定提供之結束條件的後置條件合約,以及在條件為 false 時所要顯示的訊息。Specifies a postcondition contract for a provided exit condition and a message to display if the condition is false.

Ensures(Boolean)

指定封入方法或屬性的後置條件合約。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);
static member Ensures : bool -> unit
Public Shared Sub Ensures (condition As Boolean)

參數

condition
Boolean

要測試的條件運算式。The conditional expression to test. 運算式可以包含 OldValue<T>(T)ValueAtReturn<T>(T)Result<T>() 值。The expression may include OldValue<T>(T), ValueAtReturn<T>(T), and Result<T>() values.

屬性

範例

下列範例顯示如何使用 Ensures 方法,以確保傳回預期的值。The following example shows how to use the Ensures method to ensure that an expected value is returned. 這個程式碼範例是針對 ContractClassAttribute 類別所提供的較大範例的一部分。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

備註

condition 參數會指定當封入方法或屬性正常傳回時,預期會 true 的後置條件。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.

  • 您必須使用二進位重寫工具(可在 MSDN DevLabs 網站上的程式代碼合約取得),以執行這個後置條件的執行時間強制執行。You must use the binary rewriter (available at Code Contracts on the MSDN DevLabs Web site) for run-time enforcement of this postcondition.

Ensures(Boolean, String)

指定提供之結束條件的後置條件合約,以及在條件為 false 時所要顯示的訊息。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);
static member Ensures : bool * string -> unit
Public Shared Sub Ensures (condition As Boolean, userMessage As String)

參數

condition
Boolean

要測試的條件運算式。The conditional expression to test. 運算式可以包含 OldValue<T>(T)Result<T>() 值。The expression may include OldValue<T>(T) and Result<T>() values.

userMessage
String

當運算式不是 true 時所顯示的訊息。The message to display if the expression is not true.

屬性

備註

condition 參數會指定當封入方法或屬性正常傳回時,預期會 true 的後置條件。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.

  • 您必須使用二進位重寫工具(可在 MSDN DevLabs 網站上的程式代碼合約取得),以執行這個後置條件的執行時間強制執行。You must use the binary rewriter (available at Code Contracts on the MSDN DevLabs Web site) for run-time enforcement of this postcondition.

  • 如果 userMessage 不是常數位串常值,則工具可能無法理解合約。If userMessage is not a constant string literal, the contract may not be understood by tools.

適用於