Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Contract Class

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
Assemblies:   mscorlib (in mscorlib.dll)
  System.Diagnostics.Contracts (in System.Diagnostics.Contracts.dll)

'Declaration
Public NotInheritable Class Contract

The Contract type exposes the following members.

  NameDescription
Public methodStatic memberSupported by Portable Class LibraryAssert(Boolean)Checks for a condition; if the condition is false, follows the escalation policy set for the analyzer.
Public methodStatic memberSupported by Portable Class LibraryAssert(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 memberSupported by Portable Class LibraryAssume(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 memberSupported by Portable Class LibraryAssume(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 memberSupported by Portable Class LibraryEndContractBlockMarks the end of the contract section when a method's contracts contain only preconditions in the if-then-throw form.
Public methodStatic memberSupported by Portable Class LibraryEnsures(Boolean)Specifies a postcondition contract for the enclosing method or property.
Public methodStatic memberSupported by Portable Class LibraryEnsures(Boolean, String)Specifies a postcondition contract for a provided exit condition and a message to display if the condition is false.
Public methodStatic memberSupported by Portable Class LibraryEnsuresOnThrow(Of TException)(Boolean)Specifies a postcondition contract for the enclosing method or property, based on the provided exception and condition.
Public methodStatic memberSupported by Portable Class LibraryEnsuresOnThrow(Of 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 memberSupported by Portable Class LibraryExists(Int32, Int32, Predicate(Of Int32))Determines whether a specified test is true for any integer within a range of integers.
Public methodStatic memberSupported by Portable Class LibraryExists(Of T)(IEnumerable(Of T), Predicate(Of T))Determines whether an element within a collection of elements exists within a function.
Public methodStatic memberSupported by Portable Class LibraryForAll(Int32, Int32, Predicate(Of Int32))Determines whether a particular condition is valid for all integers in a specified range.
Public methodStatic memberSupported by Portable Class LibraryForAll(Of T)(IEnumerable(Of T), Predicate(Of T))Determines whether all the elements in a collection exist within a function.
Public methodStatic memberSupported by Portable Class LibraryInvariant(Boolean)Specifies an invariant contract for the enclosing method or property.
Public methodStatic memberSupported by Portable Class LibraryInvariant(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 memberSupported by Portable Class LibraryOldValue(Of T)Represents values as they were at the start of a method or property.
Public methodStatic memberSupported by Portable Class LibraryRequires(Boolean)Specifies a precondition contract for the enclosing method or property.
Public methodStatic memberSupported by Portable Class LibraryRequires(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 memberSupported by Portable Class LibraryRequires(Of 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 memberSupported by Portable Class LibraryRequires(Of 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 memberSupported by Portable Class LibraryResult(Of T)Represents the return value of a method or property.
Public methodStatic memberSupported by Portable Class LibraryValueAtReturn(Of T)Represents the final (output) value of an out parameter when returning from a method.
Top

  NameDescription
Public eventStatic memberSupported by Portable Class LibraryContractFailedOccurs 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.

For tools and detailed instructions for using code contracts, see Code Contracts on the MSDN DevLabs Web site.

Important noteImportant

You must use a binary rewriter to insert run-time enforcement of contracts. Otherwise, contracts such as the Contract.Ensures method can only be tested statically and will not throw exceptions during run time if a contract is violated. You can download the binary rewriter CCRewrite from Code Contracts on the MSDN DevLabs Web site. CCRewrite comes with a Visual Studio add-in that enables you to activate run-time contract enforcement from the project Properties page. The binary rewriter and the Visual Studio add-in do not ship with Visual Studio 2010 or the Windows SDK.

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2015 Microsoft