新的“动态”是否? C# 4.0 关键字弃用了“var”; 关键词?
当 C# 4.0 出现时,我们有了动态关键字,如 Anders Hejlsberg 的精彩演示中所述,(C# 的发展速度比我能跟上的要快。我没有太多时间来熟悉 var 关键字)
我还需要 var 关键字吗? 有什么是 var 可以做的,而 Dynamic 不能做的吗?
var x = SomeFunctionThatIKnowReturnsSomeKindOfList();
// do something with x
dynamic x = SomeFunctionThatIKnowReturnsSomeKindOfList();
// do something with x
When C# 4.0 comes out and we have the dynamic keyword as described in this excellent presentation by Anders Hejlsberg, (C# is evolving faster than I can keep up.. I didn't have much time to acquaint myself with the var keyword)
Would I still need the var keyword ? Is there anything that var can do.. that dynamic can't?
var x = SomeFunctionThatIKnowReturnsSomeKindOfList();
// do something with x
dynamic x = SomeFunctionThatIKnowReturnsSomeKindOfList();
// do something with x
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,他们非常不同。
var
表示“在编译时推断变量的类型” - 但它仍然完全静态绑定。动态
意味着“假设我可以用这个变量做任何我想做的事情” - 即编译器不知道哪些操作可用,而 DLR 将计算出实际调用的内容 表示执行时。我希望很少使用
dynamic
- 仅当我真正想要动态行为时:var
让您在编译时捕获拼写错误等No, they're very different.
var
means "infer the type of the variable at compile-time" - but it's still entirely statically bound.dynamic
means "assume I can do anything I want with this variable" - i.e. the compiler doesn't know what operations are available, and the DLR will work out what the calls really mean at execution time.I expect to use
dynamic
very rarely - only when I truly want dynamic behaviour:var
lets you catch typos etc at compile-timeDynamic和var代表了两种完全不同的思想。
var
Var本质上是要求编译器根据赋值语句右侧的表达式来确定变量的类型。 然后,该变量将被完全视为显式声明为表达式的类型。 例如,以下两个语句是等效的。
这里要注意的关键是“var”是 100% 类型安全的,并且是编译时操作
动态
动态在很多方面与 var 完全相反。 使用动态本质上消除了特定变量的所有类型安全性。 它在很多方面都没有类型。 当您调用变量的方法或字段时,将在运行时确定如何调用该字段。 例如,
这里要注意的关键是“动态”不是类型安全的,而是运行时操作
Dynamic and var represent two completely different ideas.
var
Var essentially asks the compiler to figure out the type of the variable based on the expression on the right hand side of the assignment statement. The variable is then treated exactly as if it were explicitly declared as the type of the expression. For example the following two statements are equivalent
The key to take away here is that "var" is 100% type safe and is a compile time operation
dynamic
Dynamic is in many ways the exact opposite of var. Using dynamic is essentially eliminating all type safety for thet particular variable. It many ways it has no type. When you call a method or field on the variable, the determination on how to invoke that field occurs at runtime. For example
The key to take away here is that "dynamic" is not type safe and is a runtime operation
是的,您仍然需要 var:
Var 是一个变量,其类型将由编译器推断。
dynamic 将在运行时分配其类型
,因此:
将其类型推断为字符串类型,这样做智能将为您提供字符串可以使用的所有方法,例如,
其中 as:
不会有其类型类型推断直到运行时,因为编译器还不知道它是什么类型,但仍然会让你这样做:
但是当它调用你需要的方法时,它可能会失败,因为类型错误并且方法不存在。
Yes you will still need var:
Var is a variable whose type will be inferred by the compiler.
dynamic will have its type assigned at runtime
So:
will have its type inferred as a string type in doing so intellisence will give you all the methods that string can use like,
Where as:
won't have its type inferred untill runtime because the complier dosn't know what type it is yet, but will still let you do:
but when it calls the method that you need it may fail because the type is wrong and the method isn't there.