views:

214

answers:

1

Hi,

In VS2010 and .NET 4.0, I see the shortcuts in intellisense for adding contracts to my code (Eg cr, crr) but when I tab to add these in, the code (Such as Contract.Requires) does not have the valid assembly so there is no intellisense (The type can't be found basically).

How exactly do I get up and running with code contracts?

EDIT: All the methods exist in System.Diagnostics.Contracts, but I thought that I would be using attributes throughout? Also, there are so many different .dlls for the contracts available!

Thanks

+2  A: 

The assembly is just mscorlib - and Contract is in the System.Diagnostics.Contracts namespace.

<plug> For some more information about Code Contracts, you could buy the second edition of C# in Depth and read chapter 15. (That chapter was available free, but isn't now I'm afraid.) </plug>

Or of course you could read the docs too, as they're pretty good :)

If you find you're missing the System.Diagnostics.Contracts namespace, it's worth checking that you really are targeting .NET 4 - if you create a .NET 3.5 project in VS2010, that won't have Code Contracts available (without adding an explicit assembly reference, anyway).

Jon Skeet
Is the chapter still available for free? I can't download it.
Martin
@Martin: Rats - no it's not, unfortunately. Manning has changed it to be chapter 1 that's free. Sorry! Will edit.
Jon Skeet