Breyta

Deila með


Contract.Invariant Method

Definition

Specifies a contract for an enclosing method or property.

Overloads

Invariant(Boolean)

Specifies an invariant contract for the enclosing method or property.

Invariant(Boolean, String)

Specifies an invariant contract for the enclosing method or property, and displays a message if the condition for the contract fails.

Invariant(Boolean)

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

Specifies an invariant contract for the enclosing method or property.

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

Parameters

condition
Boolean

The conditional expression to test.

Attributes

Remarks

Invariant contracts are contained within a method that is identified by the ContractInvariantMethodAttribute attribute; typically, the method is named ObjectInvariant.

  • This contract can be specified only in a dedicated invariant method that is declared on a class. If the method is not sealed, it should refer only to protected members, not private members, so that subclasses can be sure to maintain the invariants.

  • This contract is not exposed to clients; therefore, it may reference members that are less visible than the enclosing method.

  • You must use the binary rewriter for run-time enforcement of this invariant.

  • Invariants are conditionally defined based on the presence of the CONTRACTS FULL symbol. During run-time checking, invariants are checked at the end of each public method. If an invariant mentions a public method in the same class, the invariant check that would normally happen at the end of that public method is disabled and checked only at the end of the outermost method call to that class. This also happens if the class is re-entered because of a call to a method on another class.

Applies to

Invariant(Boolean, String)

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

Specifies an invariant contract for the enclosing method or property, and displays a message if the condition for the contract fails.

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

Parameters

condition
Boolean

The conditional expression to test.

userMessage
String

The message to display if the condition is false.

Attributes

Remarks

Invariant contracts are contained within a method that is identified by the ContractInvariantMethodAttribute attribute; typically, the method is named ObjectInvariant.

  • This contract can be specified only in a dedicated invariant method that is declared on a class.

  • This contract is not exposed to clients; therefore, it may reference members that are less visible than the enclosing method.

  • You must use the binary rewriter for run-time enforcement of this invariant.

  • Invariants are conditionally defined on the CONTRACTS FULL symbol. During run-time checking, invariants are checked at the end of each public method. If an invariant mentions a public method in the same class, the invariant check that would normally happen at the end of that public method is disabled and checked only at the end of the outermost method call to that class. This also happens if the class is re-entered because of a call to a method on another class.

Applies to