动态关键字的问题关键字、动态、问题

2023-09-02 11:58:42 作者:┽岁月流年

请告诉我在哪个版本的动态关键字出台?我发现在VS2010奇怪的行为。我设定的目标框架3.5。但没有编译器错误。只是箱子,目标框架控制台应用程序到.NET 3.5,并使用动态关键字。

pls tell me in which version dynamic keyword is introduced ? I found strange behavior in VS2010. I set target framework to 3.5. But there is no compiler error. just crate a console application with target framework to .net 3.5 and use dynamic keyword .

推荐答案

动态关键字被引入作为C#4.0语言的一部分 - 编译器自带的VS 2010它是一种语言功能,不需要运行时支持(据我所知),因此一旦符合C#4.0编译器,不应该与早期运行时版本有任何问题。更改目标框架在VS 2010中不切换编译器(它保持在4.0) - 只有在使用功能,针对新库或运行时,我们会收到编译器错误。例如,在VS 2008中,你可以使用拉姆达EX pressions或var关键字进行目标运行时的2.0,但扩展方法没有用,因为扩展属性是3.5组件的一部分。

Dynamic keyword was introduced as part of C# 4.0 language - the compiler comes with VS 2010. Its a language feature and does not need runtime support (AFAIK) hence once complied with C# 4.0 compiler, shouldn't have any issue with earlier version of runtime. Changing target framework in VS 2010 does not switch the compiler (which remains at 4.0) - we will receive compiler error only if you use feature that targets new library or runtime. For example, in VS 2008, you could use lambda expressions or var keyword for target runtime 2.0 but extension methods were not available because extension attribute was part of 3.5 assembly.

编辑:以上是错误的 - 动态关键字要求框架4.0 我不能在VS2010即使编译时的目标FX改为3.5。我认为,OP可能没有使用动态无功在code,使编译器的优化会删除它使得OP后认为,其工作。

Above is wrong - dynamic keyword requires Framework 4.0. I couldn't even compile in VS2010 when target fx was changed to 3.5. I believe that OP might not have used the dynamic var later in the code so that compiler optimization would have removed it making OP believe that its working.