System.Diagnostics.Contracts.Contract Class

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

See Also: Contract Members


public static class Contract


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 tp:// on the MSDN DevLabs Web site.


You must use a binary rewriter to insert run-time enforcement of contracts. Otherwise, contracts such as the erload:System.Diagnostics.Contracts.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 tp:// 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 vs_dev10_long or the Windows SDK.


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