views:

605

answers:

5

Hello, I've been seeing a lot of C# 4.0 changes as of late. I really like some of them. Also though, I do not want to move on to .Net 4.0 for compatibility reasons just yet.

So, is there a comprehensive list of new C# 4.0 language features that will work on .Net 3.5 or lower?

As an example, do default parameters require the .NET 4.0 CLR, or are they a compiler feature? It's possible to use automatic properties (a C# 3.0 feature) and still target .NET 2.0, since that doesn't require Framework support, but not to use LINQ expressions, since that does require Framework 3.5.

Please don't say "of course C#4.0 won't work in .Net 3.5 cause it's older"

+2  A: 

I know that covariance and contravariance had some support through IL annotations + and - in the type parameters in the CLR 2.0. Not sure about the other features though.

Martinho Fernandes
+20  A: 

The C# compiler in 4.0 ships with a new multitargeting feature. The short version is that it will produce an assembly with whatever metadata version is present in the reference that defines System.Object (usually mscorlib.dll). This allows you to use it to compile assemblies for 2.0 and 3.5, as well as various versions of Silverlight. This feature was introduced in support of the multitargeting in Visual Studio 2010.

Therefore, you can use the C# 4.0 compiler to compile 3.5 assemblies, and make use of whatever C# 4 features you want, so long as there is no particular dependency on 4.0 libraries. For example, named arguments and optional parameters will work fine, because they don't use any 4.0 framework features. Dynamic, however, does, and so out of the box it won't work on 3.5.

There is a post about this here, that covers the basics.

http://blogs.msdn.com/ed_maurer/archive/2010/03/31/multi-targeting-and-the-c-and-vb-compilers.aspx

The short list (from Ed's post) is:

  • named arguments and optional parameters
  • certain COM syntax improvements (e.g., "omit ref")
  • generic variance

Things that won't work include dynamic and no-pia (our two biggest outlays in terms of time spent implementing them). The former requires framework support and the latter requires runtime support.

Chris Burrows
Great answer, but I guess what I'm looking to know then is what features in C#4.0 do not rely on a feature of .Net 4.0
Earlz
This is great. One important point from Ed's post re: generic variance is that if you target 3.5, `IEnumerable<T>` will not participate in the variance feature, so you won't be able to get rid of all your annoying `Cast<T>` calls until you can target the 4.0 runtime.
Daniel Earwicker
@Earlz - isn't that the point of the bullet list in this answer?
Daniel Earwicker
Also kudos to Ed Maurer waiting 18 months to update his (very interesting) blog!
Daniel Earwicker
+! @Daniel, yes, but that was edited in.. and also a nice blog post. Does this cover basically all the new features though?
Earlz
@Earlz, yes it does.
Chris Burrows
A: 

Considering that default parameters were already a feature of VB.NET, it is possible that C# just implemented it at the compiler level. However the CLR team could have implemented a new way entirely of doing it.

You should ready modify your question to state "what features of C# 4.0 are compiler changes that would apply to .NET 3.5 framework"

Aequitarum Custos
+2  A: 

It's not a list, nor is it definitive, but as an example, I just set up a new VS2010 project targetting .NET 3.5 and successfully created a method with default parameters, and called it with named arguments, both of which are C# 4.0 features.

Roger Lipscombe
A: 

I'm thinking of the changes to the "old" spec... the lock change won't be available, since that requires a new overload - but the "field-like events" change should work. I'll have to check what the compiler emits ;-p

And sure enough:

using System;
class Program
{
    public event EventHandler SomeHandler;
    static void Main() { }
}

gets the new-style accessors ;-p

Marc Gravell
What new accessors?
Earlz
@Marc: what is the "lock change" you're talking about.
Martinho Fernandes
@Earlz - see the link; the field-like event implementation has changed
Marc Gravell
@Martinho http://blogs.msdn.com/ericlippert/archive/2009/03/06/locks-and-exceptions-do-not-mix.aspx
Marc Gravell