ReSharper and Code Contracts

I just ran into this today. It makes ReSharper shut up about possible null references when you use Code Contracts in your C#/VB projects.

The XML file provided there is not complete, however. I’ve added the ContractInvariantMethodAttribute and ContractClassForAttribute attributes to the annotations so that ReSharper won’t claim that your contract classes and invariant methods are unused.

The XML is here.

Just drop this into C:\Program Files (x86)\JetBrains\ReSharper\v5.1\Bin\ExternalAnnotations\mscorlib as Microsoft.Contracts.xml or so.

Checklist for Type Design

Please, please, please consider the following when you design your types:

  • Is it an internal type? Does it make sense to expose it to the public?
  • Does it make sense to inherit the type? Should it be open, abstract, or sealed?
  • Does it support value equality? Should you implement IEquatable<T>?
  • Does it support comparison? Should you implement IComparable<T>?
  • Should you implement operators for the above points?
  • If you do implement the above three points, implement GetHashCode and Equals!
  • Does the type have a textual representation? Should you implement ToString?
  • Is it a small piece of data? Should it be a value type?
  • Should it be immutable? (If it’s a value type, in 99% of cases, it should!)

These guidelines of course apply to all .NET languages; C#, F#, VB.NET, etc.