4

I want to jump into coding by contract. I got VS2010 (with the C# 4.0 compiler) but I have to target the 3.5 framework.

What 3rd party code by contract library has classes and interface the most like the .NET 4.0 ones?

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
  • Have you tried just using [Microsoft's](http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx)? It works by rewriting the MSIL, so it's not really a "library", and I expect it would work just fine on 3.5. – Stephen Cleary May 14 '10 at 20:40
  • When I add "using System.Diagnostics.Contracts;" It says, Contracts isn't there. MSDN says it is in mscorlib.dll. I'd be surprised if I can reference the 4.0 version of that from a project targeting 3.5. – MatthewMartin May 14 '10 at 20:47
  • Try referencing the Microsoft.Contracts dll. That's where the 3.5 classes used to be, anyway. – Stephen Cleary May 14 '10 at 20:57

1 Answers1

9

From the user guide:

Starting with the CLR v4, the Contract class and related types reside in mscorlib.dll. Prior to CLR v4, these types appear in a separate assembly called Microsoft .Contracts. dll that is installed under %PROGRAMFILES%/Microsoft/Contracts/PublicAssemblies. You can need to add a reference to this assembly if you are compiling against a pre 4.0 CLR.

You may have some interesting issues if you want to use the same built assembly against both 3.5 and 4.0 (I don't know - it may just work) but if you're just using 3.5, that should be okay.

The extension installs into VS2008 as well as VS2010, I believe.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • You can build the same assembly against multiple CLR versions? Actually... why would you want to? – Powerlord May 14 '10 at 20:59
  • @R. Bemrose: If you have some applications running against .NET 3.5 and some against .NET 4.0, you may well only want to build once... if it works. – Jon Skeet May 14 '10 at 22:02
  • I believe the relevant classes are compiled into *your* assembly by the CC rewriter, so that there shouldn't be any versioning problems. (At least this is what I've gathered from Reflector!) – porges May 20 '10 at 21:22
  • @Porges: That's definitely the case for a lot of the stuff, but I wasn't sure whether it was *all* classes... – Jon Skeet May 20 '10 at 21:34