新的“动态”是否? C# 4.0 关键字弃用了“var”; 关键词?

发布于 2024-07-09 05:25:42 字数 406 浏览 8 评论 0原文

当 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

维持三分热 2024-07-16 05:25:42

不,他们非常不同。

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-time
  • statically bound code is always going to run faster than dynamically bound code (even if the difference becomes reasonably small)
  • statically bound code gives more compile-time support beyond just errors: you can find call hierarchies, refactoring will work better, Intellisense is available etc
生生不灭 2024-07-16 05:25:42

Dynamic和var代表了两种完全不同的思想。

var

Var本质上是要求编译器根据赋值语句右侧的表达式来确定变量的类型。 然后,该变量将被完全视为显式声明为表达式的类型。 例如,以下两个语句是等效的。

var a = "foo";
string a = "foo";

这里要注意的关键是“var”是 100% 类型安全的,并且是编译时操作

动态

动态在很多方面与 var 完全相反。 使用动态本质上消除了特定变量的所有类型安全性。 它在很多方面都没有类型。 当您调用变量的方法或字段时,将在运行时确定如何调用该字段。 例如,

dynamic d = SomeOperation();
d.Foo(); // Will this fail or not?  Won't know until you run the program

这里要注意的关键是“动态”不是类型安全的,而是运行时操作

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

var a = "foo";
string a = "foo";

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

dynamic d = SomeOperation();
d.Foo(); // Will this fail or not?  Won't know until you run the program

The key to take away here is that "dynamic" is not type safe and is a runtime operation

一花一树开 2024-07-16 05:25:42

是的,您仍然需要 var:

Var 是一个变量,其类型将由编译器推断。

dynamic 将在运行时分配其类型

,因此:

Var i = "Hello World"

将其类型推断为字符串类型,这样做智能将为您提供字符串可以使用的所有方法,例如,

i.Split("/")

其中 as:

dynamic i = "Hello World"

不会有其类型类型推断直到运行时,因为编译器还不知道它是什么类型,但仍然会让你这样做:

i.Split("/")

但是当它调用你需要的方法时,它可能会失败,因为类型错误并且方法不存在。

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:

Var i = "Hello World"

will have its type inferred as a string type in doing so intellisence will give you all the methods that string can use like,

i.Split("/")

Where as:

dynamic i = "Hello World"

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:

i.Split("/")

but when it calls the method that you need it may fail because the type is wrong and the method isn't there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文