Code Contracts for .NET Is Available for Download

| by Abel Avram Follow 5 Followers on Feb 24, 2009. Estimated reading time: 1 minute |

A note to our readers: You asked so we have developed a set of features that allow you to reduce the noise: you can get email and web notifications for topics you are interested in. Learn more about our new features.

Code Contracts is the .NET implementation of the Design by Contract concept. While it was supposed to be delivered with .NET 4.0, Code Contracts is already available for download from DevLabs. Contracts impose certain restrictions on using APIs, making programming safer, having more validations and resulting in fewer unexpected errors during runtime.

Design by Contract, or Programming by Contract, is not a new concept in programming, many languages already having such implementations. Code Contract brings this programming paradigm to .NET. The basic idea is to establish a contract between the two parties involved: the client (caller) and the supplier (API called). By adhering to the terms of the contract, the client is more confident the supplier will supply expected results, and the supplier will be more confident that the client will make appropriate calls.

There are three types of contract conditions available for Code Contracts: pre-conditions, object invariants, and post-conditions. Pre-conditions are used, for example, to verify that a constructor or a method is called using the right parameter values. A post-condition is used to verify that a value returned is appropriate. Object invariants are used to enforce certain conditions on objects to make sure they don’t obtain illegal values.

While this sounds very similar to unit testing, what’s news is that those validations can be performed at build time. The compiler will make specific checks via Contracts to ensure that all the conditions specified are met, issuing warning if they are not. The programmer can immediately see if a condition is not going to be met and take corrective measures before running the code.

Currently, the DevLabs team works on including Contracts in the documentation generated for projects. By reading an API documentation, anyone will be able to see all the contract conditions specified for it.

The end result of using Code Contracts is better and safer code, more appropriate usage of external APIs, in the end better software.

Useful links: Code Contracts online documentation, Microsoft Research Code Contracts website, related forum.

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Impressive by Mohamed Faramawi

I think this will minimize the need to write unit tests to validate method behaviors when an invalid input is passed as passing an invalid input is not supported.
It looks like injected unit tests in runtime code.. however the contracts should be unit tested somehow.. but i don't think it will be that hard.
Can't wait to use this

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss

Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and don't miss out on content that matters to you