So we've finally got VS2010 on some developer stations at work and can use the C# 4.0 features. Although most of what we develop will still have to target .Net 3.5 for the time being.
When I start a new project and set the target to .Net 3.5, it still allows me to use C# 4.0 such as dynamic. Can you therefore use C#4.0 features whilst targetting .net 3.5 and will these features work in environments where .Net 4.0 is not available?
Thanks.
Best Answer
dynamic
code will not compile if you target the .NET 3.5 framework.To be more clear, the compiler will allow you to define and assign a
dynamic
variable, such as:That one line of code will compile, because
dynamic
just compiles toobject
as far as types are concerned. But if you then try to do anything with that variable, as in:... then the compiler would have to generate code to discover/coerce the real type, which it cannot do; you'll get the following compile errors:
The C# 4 compiler relies on the DLR and specifically the Microsoft.CSharp assembly for everything related to
dynamic
. These aren't available in .NET 3.5. So the answer is no, you cannot usedynamic
when targeting Framework version 3.5.