Contract Class

Contains static methods for representing program contracts such as preconditions, postconditions, and object invariants.

System.Object
  System.Diagnostics.Contracts.Contract

Namespace:  System.Diagnostics.Contracts
Assembly:  mscorlib (in mscorlib.dll)

public static class Contract

The Contract type exposes the following members.

  NameDescription
Public methodStatic memberAssert(Boolean)Checks for a condition; if the condition is false, follows the escalation policy set for the analyzer.
Public methodStatic memberAssert(Boolean, String)Checks for a condition; if the condition is false, follows the escalation policy set by the analyzer and displays the specified message.
Public methodStatic memberAssume(Boolean)Instructs code analysis tools to assume that the specified condition is true, even if it cannot be statically proven to always be true.
Public methodStatic memberAssume(Boolean, String)Instructs code analysis tools to assume that a condition is true, even if it cannot be statically proven to always be true, and displays a message if the assumption fails.
Public methodStatic memberEndContractBlockMarks the end of the contract section when a method's contracts contain only preconditions in the if-then-throw form.
Public methodStatic memberEnsures(Boolean)Specifies a postcondition contract for the enclosing method or property.
Public methodStatic memberEnsures(Boolean, String)Specifies a postcondition contract for a provided exit condition and a message to display if the condition is false.
Public methodStatic memberEnsuresOnThrow<TException>(Boolean)Specifies a postcondition contract for the enclosing method or property, based on the provided exception and condition.
Public methodStatic memberEnsuresOnThrow<TException>(Boolean, String)Specifies a postcondition contract and a message to display if the condition is false for the enclosing method or property, based on the provided exception and condition.
Public methodStatic memberExists(Int32, Int32, Predicate<Int32>)Determines whether a specified test is true for any integer within a range of integers.
Public methodStatic memberExists<T>(IEnumerable<T>, Predicate<T>)Determines whether an element within a collection of elements exists within a function.
Public methodStatic memberForAll(Int32, Int32, Predicate<Int32>)Determines whether a particular condition is valid for all integers in a specified range.
Public methodStatic memberForAll<T>(IEnumerable<T>, Predicate<T>)Determines whether all the elements in a collection exist within a function.
Public methodStatic memberInvariant(Boolean)Specifies an invariant contract for the enclosing method or property.
Public methodStatic memberInvariant(Boolean, String)Specifies an invariant contract for the enclosing method or property, and displays a message if the condition for the contract fails.
Public methodStatic memberOldValue<T>Represents values as they were at the start of a method or property.
Public methodStatic memberRequires(Boolean)Specifies a precondition contract for the enclosing method or property.
Public methodStatic memberRequires(Boolean, String)Specifies a precondition contract for the enclosing method or property, and displays a message if the condition for the contract fails.
Public methodStatic memberRequires<TException>(Boolean)Specifies a precondition contract for the enclosing method or property, and throws an exception if the condition for the contract fails.
Public methodStatic memberRequires<TException>(Boolean, String)Specifies a precondition contract for the enclosing method or property, and throws an exception with the provided message if the condition for the contract fails.
Public methodStatic memberResult<T>Represents the return value of a method or property.
Public methodStatic memberValueAtReturn<T>Represents the final (output) value of an out parameter when returning from a method.
Top

  NameDescription
Public eventStatic memberContractFailedOccurs when a contract fails.
Top

Code contract classes let you specify preconditions, postconditions, and object invariants in your code. Preconditions are requirements that must be met when entering a method or property. Postconditions describe expectations at the time the method or property code exits. Object invariants describe the expected state for a class that has no condition problems. For more information about preconditions, postconditions, and object invariants, see Code Contracts.

Important noteImportant

You must install a Visual Studio add-in to enforce contracts. The Code Contracts Premium Edition add-in lets you specify static and run-time checking of code contracts on the project Properties page. If you do not enable run-time checking, contracts such as the Contract.Ensures method will not throw exceptions during run time if a contract is violated. The Visual Studio add-in does not ship with Visual Studio 2010 or the Windows SDK.

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft