Search code examples
c#-4.0abstract-classcode-contracts

Contract class should be an abstract class


The following code gives me the warning Contract class 'FooContracts' should be an abstract class. From all the examples I've read online (e.g. http://www.infoq.com/articles/code-contracts-csharp), this should work (presumably without compiler warnings).

[ContractClass(typeof(FooContracts))]
public interface IFoo {
  void Bar(string foo);
}

[ContractClassFor(typeof(IFoo))]
internal sealed class FooContracts : IFoo {
  void IFoo.Bar(string foo) {
    Contract.Requires(foo != null);
  }
}

I'm in Visual Studio 2010, with the following settings in the Code Contracts section of the project's properties:

  • Perform Runtime Contract Checking (set to Full)
  • Perform Static Contract Checking (under Static Checking)
  • Check in Background

I also defined the CONTRACTS_FULL compilation symbol to make ReSharper shut up.

Am I missing something to make this compile without warnings?


Solution

  • Section 2.8 of the code contracts manual specifically states that it should be an abstract class:

    The tools expect that the contract class is abstract and implements the interface it is providing contracts for.