Code contracts is a Microsoft Research project that is set to “..provide a language-agnostic way to express coding assumptions in .NET programs.” What this basically means is that Microsoft Research is creating the next generation of defensive programming into the .Net Framework. Code contracts provides a language based way of specifying and checking for invariants, pre and post-conditions in objects. Everyone has written defensive code like the following:
public void MyMethod(Foo foo, int length)
{
if (foo == null)
{
throw new ArgumentNullException("foo");
}
if (length < 10)
{
throw new ArgumentOutOfRangeException("length", "Length cannot be less then 10");
}
}
One of the problems with this code is that there is no comments to the parameters, so if you do not look at the implementation you have no way of knowing that Foo cannot be null and length has to be above 9. In some way this is documented by the code, but you cannot expect that everyone looks at the code. This is one of the issues Code Contracts can help with, following is the same method with Code Contracts:
public void MyMethod(Foo foo, int length)
{
Contract.Requires<ArgumentNullException>(foo != null, "foo");
Contract.Requires<ArgumentOutOfRangeException>(length > 9, "Length cannot …");
}
The result is the same, if you invoke MyMethod where Foo is null or length is less then 9 an exception will be throw, but wow anyone that has the Code Contracts plugin installed will be presented with the following warnings inside Visual Studio when they try to invoke MyMethod with parameters that does not follow the contracts:


(Some Paint manipulation has been done to show both errors at the same time )
What have been shown here is the static analysis (Design time) part of Code Contracts, via project properties in visual studio you can decide what part of the contracts you want at runtime. Since there is some overhead in both pre and post-conditions you can turn both off and Code Contracts will not modify the MSIL to include the contracts, thereby eliminating the overhead. Following are the two basic functions that comes with code contracts:
Requires
Contract.Requires insure preconditions, this can be parameter and object state validation, examples:
Contract.Requires<ArgumentNullException>(foo != null, "foo");
Contract.Requires(length > 9);
Contract.Requires(length > 9, "Length cannot be less then 10");
Ensures
Contract.Ensures sets postconditions, this is mainly to ensure object state at the end of a method and validate the return value of a method, examples:
public int MyMethod(Foo foo, int length)
{
Contract.Ensures(foo.Price == Contract.OldValue<double>(foo.Price));
Contract.Ensures(Contract.Result<double>() > length);
…
}
There are a number of other functions in code contracts like: assert, assume, ContractInvariantMethod, ContractClass and ContractClassFor, but I have stuck with the basics here. Following are some snippets that are included in the Code Contract plugin:
| cim |
[ContractInvariantMethod] void ObjectInvariant() { Contract.Invariant(false); }
|
| cr |
Contract.Requires(false);
|
| crn |
Contract.Requires(arg != null);
|
crsn
|
Contract.Requires(!String.IsNullOrEmpty(arg));
|
| ce |
Contract.Ensures();
|
| cen |
Contract.Ensures(Contract.Result<string>() != null);
|
cesn
|
Contract.Ensures(!String.IsNullOrEmpty( Contract.Result<string>()));
|
cam
|
Contract.Assume(false);
|
cca
|
Contract.Assert(false);
|
cintf
|
[ContractClass(typeof(IFooContract))]
public partial interface IFoo
{
}
[ContractClassFor(typeof(IFoo))]
abstract class IFooContract : IFoo
{
}
|
For more detailed information on Code Contracts see http://msdn.microsoft.com/en-us/magazine/ee236408.aspx
UPDATE:
What I might not have made clear is that Code Contracts will compile and run on any machine, but to get “design time” warnings and the ability to decide if contracts should be included or not, then you will need the plugin .