开发者

C# 4.0 and .Net 3.5

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 fo开发者_JAVA技巧r 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.


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:

dynamic x = 3;

That one line of code will compile, because dynamic just compiles to object as far as types are concerned. But if you then try to do anything with that variable, as in:

Console.WriteLine(x);

... 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:

  1. Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
  2. One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?

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 use dynamic when targeting Framework version 3.5.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜