Most likely, the InfoQ article you are linking to is incorrect. This is based on the "early access" of the C # edition of Depth, so the implementation of code contracts has probably changed between how the chapter / article was originally written and .NET 4 was released.
The following code should work:
[ContractClass(typeof(FooContracts))] public interface IFoo { void Bar(string foo); } [ContractClassFor(typeof(IFoo))] internal abstract class FooContracts : IFoo { void IFoo.Bar(string foo) { Contract.Requires(foo != null); } }
The class of the contract must be abstract.
Scott dorman
source share