C#中的动态vs var
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1920790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dynamic vs var in C#
提问by Bhaskar
Possible Duplicate:
What’s the difference between dynamic(C# 4) and var?
What is the difference between dynamic and var keyword in .NET 4.0 (VS 2010). As per MSDN, the definition of dynamic is - Dynamic lookup allows you to write method, operator and indexer calls, property and field accesses, and even object invocations which bypass the normal static binding of C# and instead gets resolved dynamically.
.NET 4.0 (VS 2010) 中的 dynamic 和 var 关键字有什么区别。根据 MSDN,动态的定义是 -动态查找允许您编写方法、运算符和索引器调用、属性和字段访问,甚至绕过 C# 的正常静态绑定而是动态解析的对象调用。
Whereas the definition for var is - An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.
而 var 的定义是 -隐式类型的局部变量是强类型的,就像您自己声明了类型一样,但编译器会确定类型。
How is this different in the code context below:
这在下面的代码上下文中有何不同:
var a1 = new A();
a1.Foo(1);
dynamic a2 = new A();
a2.Foo(1);
采纳答案by Jon Skeet
var
means the statictype is inferred - in your case it's exactly equivalent to
var
意味着推断出静态类型 - 在您的情况下,它完全等同于
A a1 = new A();
All the binding is still done entirelystatically. If you look at the generated code, it will be exactly the same as with the above declaration.
所有绑定仍然完全静态完成。如果您查看生成的代码,它将与上面的声明完全相同。
dynamic
means that all any expression using a2
is bound at execution time rather than at compile-time, so it can behave dynamically. The compiler won't check whether the Foo
method exists - the behaviour is determined at execution time. Indeed, if the object implements IDynamicMetaObjectProvider
it could decide what to do with the call at execution time, responding to anymethod call (or other kind of use) - in other words, there doesn't have to be a "real" method called Foo
at all.
dynamic
意味着所有使用的表达式a2
都在执行时绑定,而不是在编译时绑定,因此它可以动态运行。编译器不会检查该Foo
方法是否存在——行为是在执行时确定的。事实上,如果对象实现IDynamicMetaObjectProvider
它可以决定如何处理在执行时通话,应对任何方法调用(或其他类型的使用) -换句话说,有没有被称为一个“真实”的方法,Foo
在全部。
If you look at the generated code in the dynamic situation, you'll find all kinds of weird and wonderful stuff going on to do with call sites and binders.
如果您查看动态情况下生成的代码,您会发现与调用站点和绑定器有关的各种奇怪而美妙的东西。
回答by Oded
In the var
case, A() has to have a .Foo(int)
method on it during compilation.
在这种var
情况下, A().Foo(int)
在编译期间必须有一个方法。
In the dynamic
case, it doesn't.
在这种dynamic
情况下,它没有。
回答by Klaus Byskov Pedersen
var
is type safe, in that it uses type inference.
Writing var a = new A();
is a shorthand for A a = new A();
.
A variable which is declared dynamic
is NOT type safe and the compiler does nothing to check that the methods you call on it exist.
var
是类型安全的,因为它使用类型推断。写作var a = new A();
是 的简写A a = new A();
。声明的变量dynamic
不是类型安全的,编译器不会检查您调用它的方法是否存在。