Code Contracts provide a language-agnostic way to express coding assumptions in .NET programs. The contracts take the form of preconditions, postconditions, and object invariants. Contracts act as checked documentation of your external and internal APIs.

Contracts is Microsoft's reference implementation of the "Design by contract" methodology. According to Microsoft Research, "Code Contracts bring the advantages of design-by-contract programming to all .NET programming languages." They can be checked statically at compile time or dynamically at run time, or be used to create unit tests semi-automatically.

The bulk of its functionality is implemented as a set of static library methods for writing pre-conditions, post-conditions and invariants; this allows all .NET languages to write contracts without any special extensions. All contracts are expressed as static calls to the methods Contract.Requires and Contract.Ensures. Therefore, normal utilities of the .NET tool chain such as Intellisense and compile-time checking can be applied to the task of writing contracts.

Like all language or library tags, this tag should be used for questions about issues in using this tool for writing code, or about its suitability for a particular purpose.

history | excerpt history