Sunday, 19 April 2015

.Net 4 vs 4.5

Last time we needed to verify at work if one machine had .Net 4.0 or .Net 4.5 there was some confusion, so I'll gather here some information about this.

Years ago it was simple to determine the installed Framework version/versions, it was just a matter of going to C:\Windows\Microsoft.NET\Framework and checking what folders you have there. With .Net 4.5 this is not so immediate as .Net 4.5 is an in place replacement for .Net 4.0. This means that regardless of whether you have .Net 4.0 or 4.5, the folder with the biggest number that you'll find there is v4.0.30319.

You have mainly 2 ways to check your version, checking the Registry or checking the version of clr.dll.

  • For the Registry option you'll have to check this key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client\1033\version
  • For the clr.dll, you have to bear in mind that both .Net 4.0 and 4.5 use the same CLR version, version 4.0, but anyway the dll's are not same build, so if you check the version number of clr.dll (or clrjit.dll), if the last block 4.0.30319.34000 (not sure if that's the build or the revision) is 19000 or bigger, that's .Net 4.5. Of course I didn't find this on my own, I just got the information from here

.Net 4.5 is backwards compatible, so an assembly compiled for 4.0 runs seamlessly on 4.5. I assume that one application running on 4.5 can mix assemblies compiled for 4.5 and for 4.0 without a problem On the contrary, you can not run a 4.5 assembly on .Net 4.0. If for example you are compiling an application for 4.0 and you reference an assembly compiled for 4.5, SharpDevelop will ask you to change the target of your application to 4.5.

By the way, thanks to this I've finally understood what's the use of .Net Reference Assemblies (those assemblies located in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework). As .Net 4.5 assemblies replace the 4.0 ones in the GAC, how is the compiler chain able to target 4.0 (preventing you from using classes, methods missing in 4.0)? The answer is that the compiler chain uses the Reference Assemblies (and you have there both the 4.0 and 4.5 ones) rather than the ones in the GAC. The Reference Assemblies are smaller than real ones, cause as they are used only for compilation but not for execution they have only the metadata, but the real IL code has been taken out of them.

No comments:

Post a Comment