开发者

Getting up and running with code contracts

In VS2010 and .NET 4.0, I see the shortcuts in intellisense for adding contra开发者_C百科cts 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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜