views:

1703

answers:

6

I have read countless blogs, posts and StackOverflow questions about the new features of C# 4.0. Even new WPF 4.0 features have started to come out in the open. What I could not find and will like to know:

  1. What are the major changes to CLR 4.0 from a C#/WPF developer perspective?
  2. What are the major changes to CLR 4.0 as a whole?

I think, internally, most changes are for the new dynamic languages and parallel programming. But are there any other major improvements? Because language improvements are just that, language improvements. You just need the new compiler and those features can be used with a lower version of .Net, apart from version 1.0/1.1 (at least most of them can be used).

And if the above features are the only ones, only for these features the version is changed to 4.0, which I think is 4.0 because of being based on .Net 4.0 version (i.e. after 1.0/1.1, 2.0 & 3.0/3.5). Is the version increment justified?

Edited:

As Pavel Minaev pointed out in the comments, even those two features are CLR independent. There were speed and other improvements in 3.0 and 3.5 also. So why the version increment?

+2  A: 

I don't believe there are any new IL instructions. The new CLR has improvements to things like inlining and garbage collection which do the same job as the 2.0 CLR, but better. A virtual machine (such as the CLR or JVM) is an abstract concept with multiple possible implementations. I believe CLR 4.0 is the same abstract machine as in CLR 2.0, just with an improved implementation.

Even the new dynamic stuff is just a compiler trick with new APIs (unlike in Java where it's being proposed as a new opcode.)

If I'm wrong about this, I'd love to know!

Drew Noakes
There must be one (107), Reflector keeps throwing exceptions on that :)
leppie
+3  A: 

There are a huge number of changes.

In the CLR itself, there are a few changes. The garbage collector is being changed to support concurrent collection of gen0/1 and gen2 in workstation mode. Also, there are some changes in how security is implemented. The parallel framework changes some of the CLR's implementation of the threadpool (which is not entirely managed, but part of the runtime itself). Also, there are some changes to the type system, mostly related to the new PIA COM support.

The biggest changes are probably more library/framework changes, rather than CLR changes. such as the integration of the DLR into the framework, and the new dynamic type. In terms of framework, you have the reactive framework, parallel library extensions, code contracts, tuple support, and lots of small changes (ie: Enum.TryParse, Lazy<T>, and tons of other small, but nice, improvements).

Reed Copsey
`dynamic` is a C# artifact, and is not a CLR type. And what "integration of DLR into the CLR" do you speak of? DLR is really just a library.
Pavel Minaev
True, I may edit my answer.
Reed Copsey
Edited. Some of these do require specific changes, though.
Reed Copsey
+6  A: 

One new CLR thing that I know about is a form of structural typing for interfaces, structs and delegates for the sake of NoPIA support - basically, it lets runtime treat distinct types with equivalent definitions as if they were the same - so if two assemblies A and B each have a COM-imported interface IFoo declared in them, with the same IID and same members, runtime will treat them as equivalent types; so if there's an instance some class Foo implementing [A]IFoo, you can cast it to [B]IFoo, and the cast will work.

One other thing is the ability to host several CLR versions side-by-side in a single process. You cannot host 1.x and 2.0 in one process, for example, but you can host 2.0 and 4.0. The main benefit for this is the ability to load plugins written for either CLR version concurrently.

One minor bit is that a few more exceptions have become uncatchable like StackOverflowException was in 2.0 - you cannot catch AccessViolationException anymore, for example.

Also, here is a PowerPoint presentation on CLR 4.0 from PDC 2008. It might be a bit dated now, but most stuff that's mentioned there seems to be in the betas.

Pavel Minaev
@Pavel, why can't you catch those exceptions anymore in 4? Are they obsolete?
Joan Venge
They were made uncatchable deliberately, because there is no legitimate reason for an application to catch them. For `StackOverflowException`, this is because it is a condition in which any error-handling code wouldn't be able to do much, anyway (if you're out of stack, you can't even make a method call...). For `AccessViolationException`, it is because it is by its very nature nondeterministic - an operation that would cause one can also just as well read (or, worse yet, write) some unrelated memory region; so catching it is always a design problem.
Pavel Minaev
The `AccessViolationException` may have become uncatchable because Code Access Security has been deprecated, which makes that exception deprecated: http://www.infoq.com/news/2010/01/CAS-.NET-4.0. It has never been nondeterministic, surely not in areas where you want to allow to run alien code.
Abel
`AccessViolationException` has absolutely no relation to Code Access Security (in particular, it is _not_ thrown when your code violates a CAS rule). It's just Windows-speak for a segmentation fault (http://en.wikipedia.org/wiki/Access_violation)
Pavel Minaev
A: 

I tried looking at some of the new C# stuff in Reflector, to see if there's anything new down under:

  • dynamic types are translated to objects, via compiler magic, which adds library calls to handle runtime binding.
  • Optional parameters are handled via the compiler. If you call Foo(int x = 5), without specifying a value for x, you'll see the call as Foo(5) in Reflector.

So I guess the changes are nothing you can easily spot (like generic support in CLR 2.0).

Doron Yaacoby
I did not tried dynamic types, but optional parameters work fine with even .Net 2.0. The only thing you need is the VS2010 C# Compiler. Maybe even dynamic may work. Not sure though.
Yogesh
Nope, `dynamic` needs the DLR, as well as C# binder for that in `Microsoft.CSharp`. Those only come in 4.0.
Pavel Minaev
The assemblies do only come in .NET 4.0, but that's it. You just need the compiler, the assemblies and in theory you should be able to run it on CLR 2.0.
Doron Yaacoby
A: 

Many core interface and delegate types in the CLR have been updated in support of generic covariance and contravariance. For example, IEnumerable<T> has been changed to IEnumerable<out T>.

Joel Mueller
That's not CLR (Common Language __Runtime__), that's BCL (Base Class Library). CLR is the VM.
Pavel Minaev
Fair enough. The changes to common interfaces were BCL updates to take advantage of a new feature in the CLR.
Joel Mueller
+1  A: 

For each release documentation teams create "What's new" documents.

Here is the one for C# 4.0 Beta2: What's New in Visual C# 2010

And here is the one for .NET Framework 4.0 Beta2: What's New in the .NET Framework Version 4

These are the most complete lists of changes you can find.

Alexandra Rusina