views:

91

answers:

2

Development Environment : - VS2010 - .Net Framework 4.0, 3.5, 2.0

Staging and Production Environments: - .Net Framework 3.5, 2.0

The project I'm working on is targeting .Net Framework 3.5. And today I used optional parameters feature, which is new to C#4, in this project and it worked fine. I think VS2010 is using C#4 compiler and is compiling the method with optional parameters to corresponding overloaded methods in IL.

I want to know if I can use all new C#4 features as well.

+5  A: 

You cannot use is the dynamic feature. This relies on the C# runtime and DLR DLL's which are only available on the 4.0 version of the .Net framework. Versions of the DLR are available for 3.5 but I do not believe they are compatible with the one required by the C# compiler.

Additionally you cannot use NoPIA / Embedded Interop Types in a down targetted scenario. This feature requires CLR support that was added in 4.0.

What's great about down targeting in Visual Studio 2010 though is you don't have to be aware of every limitation. If your projects are set to down target 3.5 and you use an incompatible feature, Visual Studio will produce an error.

JaredPar
I believe you also can’t use co- and contra-variance? Or will the 3.5 CLR happily play along with that?
Timwi
AFAIK the 2.0 VM already contained support for that.
Dykam
@Timwi, @Dykam is correct that CLR 2.0 already had support for co and contra variance. It was more of a bug fix for the C# and VB.Net Language.
JaredPar
It's great to know VS2010 catches any unsupported new features. Thank you all.
ReggaeMan
+1  A: 

I bumped into this a couple of weeks ago actually. I used optional parameters even though the project targeted .net 3.5. You need to be very careful of this because if you install the application on a computer that only has .net 3.5 runtime installed then your program may not run. In my case, I used the optional params and the nightly build server only had 3.5 installed so the build failed.

brandon
Your project may have failed to *build* using 3.5, but if you can successfully build it using 4 but *targeting 3.5* then the compiled code should *run* on 3.5 quite happily.
LukeH