Skip to main content

DevLabs: Code Contracts


Code Contracts

Code Contracts provide a language-agnostic way to express coding assumptions in .NET programs.

Note: There is now just one version that works with every edition of Visual Studio (except Express) and includes the static checker!

The contracts take the form of pre-conditions, post-conditions, and object invariants. Contracts act as checked documentation of your external and internal APIs. The contracts are used to improve testing via runtime checking, enable static contract verification, and documentation generation. Code Contracts bring the advantages of design-by-contract programming to all .NET programming languages. We currently provide three tools:

  • Runtime Checking. Our binary rewriter modifies a program by injecting the contracts, which are checked as part of program execution. Rewritten programs improve testability: each contract acts as an oracle, giving a test run a pass/fail indication. Automatic testing tools, such as Pex, take advantage of contracts to generate more meaningful unit tests by filtering out meaningless test arguments that don't satisfy the pre-conditions.
  • Static Checking. Our static checker can decide if there are any contract violations without even running the program! It checks for implicit contracts, such as null dereferences and array bounds, as well as the explicit contracts.
  • Documentation Generation. Our documentation generator augments existing XML doc files with contract information. There are also new style sheets that can be used with Sandcastle so that the generated documentation pages have contract sections.

Code Contracts installs if you have any edition of Visual Studio other than the Express edition. It includes the static checker and the binary rewriter (for runtime checking). It also contains a stand-alone contract library (for use with .NET projects that target a runtime earlier than version 4), the contract assembly generator, a set of contract assemblies for the .NET Framework, and user documentation.

See more DevLabs projects >


Feedback

Give feedback about Code Contracts in the DevLabs Forums.

Code Contracts

Contracts not being evaluated
Hi, I have a problem with Contracts, I'll show it below in a ficticious situation, as the real problem would be impossible to reproduce here.I have a... další
úterý, 5 22
Failed contract with string addition.
string dependencyPropertyFieldName = propertyInfo.Name + "Property"; Contract.Assert(dependencyPropertyFieldName != string.E... další
úterý, 5 22
"Call-site Requires Checking" does not work on constructors.
Hello,I think that I found a bug in Code Contracts v1.4.50327.0 (and also in previous).Consider the following scenario :I have a Class1 in a projec... další
pondělí, 5 21
Debugger gets confused when using Contract.Requires and a lambda capturing a variable
I found a problem with Code Contracts (version 1.4.50327.0) that confuses the debugger. I have in the same method a lambda expression capturing a loca... další
neděle, 5 20
Featured Video

Getting Started with Code Contracts in Visual Studio 2008

 

 

 

Get Code Contracts

Code Contracts provide a language-agnostic way to express coding assumptions in .NET programs.

Get Started

Download Code Contracts.
Read the Code Contracts documentation.


Requirements

  • Development Tools: Any version of Visual Studio other than the Express edition