Contract.OldValue<T>(T) 方法

定義

表示其在方法或屬性開始時的值。Represents values as they were at the start of a method or property.

public:
generic <typename T>
 static T OldValue(T value);
public static T OldValue<T> (T value);
static member OldValue : 'T -> 'T
Public Shared Function OldValue(Of T) (value As T) As T

類型參數

T

值的類型。The type of value.

參數

value
T

要表示的值 (欄位或參數)。The value to represent (field or parameter).

傳回

T

在方法或屬性開始時之欄位或參數的值。The value of the parameter or field at the start of a method or property.

範例

下列範例示範如何使用 OldValue 方法,以確保已更新計數。The following example shows the use of the OldValue method to ensure that a count has been updated. 這個程式碼範例是針對 ContractClassAttribute 類別所提供的較大範例的一部分。This code example is part of a larger example provided for the ContractClassAttribute class.

void IArray.Insert(int index, Object value)
{
    Contract.Requires(index >= 0);
    Contract.Requires(index <= ((IArray)this).Count);  // For inserting immediately after the end.
    Contract.Ensures(((IArray)this).Count == Contract.OldValue(((IArray)this).Count) + 1);
}
Sub Insert(ByVal index As Integer, ByVal value As [Object]) Implements IArray.Insert
    Contract.Requires(index >= 0)
    Contract.Requires(index <= CType(Me, IArray).Count) ' For inserting immediately after the end.
    Contract.Ensures(CType(Me, IArray).Count = Contract.OldValue(CType(Me, IArray).Count) + 1)

End Sub

備註

這個方法只能用在 Ensures 合約的條件運算式中。This method can be used only in the conditional expression for the Ensures contract.

適用於