将使用“无功”影响性能? [英] Will using 'var' affect performance?

查看:99
本文介绍了将使用“无功”影响性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早些时候,我问了一个关于为什么我看到这样的例子很多使用 VAR 关键字并得到了答案,虽然这仅仅是匿名类型的需要,它是用来仍然使写作code'更快'/更简单,只是因为'

Earlier I asked a question about why I see so many examples use the varkeyword and got the answer that while it is only necessary for anonymous types, that it is used nonetheless to make writing code 'quicker'/easier and 'just because'.

链路(C#3.0 - 瓦尔是不是Objec)我看到 VAR 获取的IL(你会看到它大约中途下来的文章)。

Following this link ("C# 3.0 - Var Isn't Objec") I saw that var gets compiled down to the correct type in the IL (you will see it about midway down article).

我的问题是多少,如果有的话,IL code不使用 VAR 关键字拿的,它甚至会接近上有一个可衡量的水平在code,如果它被到处使用的性能?

My question is how much more, if any, IL code does using the var keyword take, and would it be even close to having a measurable level on the performance of the code if it was used everywhere?

推荐答案

有没有额外的IL code为 VAR 关键字:产生的IL应该是相同的非匿名类型。如果编译器无法创建IL因为它无法找出你打算用什么类型,你会得到一个编译器错误。

There's no extra IL code for the var keyword: the resulting IL should be identical for non-anonymous types. If the compiler can't create that IL because it can't figure out what type you intended to use, you'll get a compiler error.

唯一的诀窍是, VAR 将推断出在哪里,如果你要手动设置类型,你可以选择一个接口或父类型完全相同的类型。

The only trick is that var will infer an exact type where you may have chosen an Interface or parent type if you were to set the type manually.

这篇关于将使用“无功”影响性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆