“var”的目的是什么?

发布于 2024-10-17 17:28:21 字数 728 浏览 2 评论 0原文

可能的重复:
var 关键字有什么意义?

不询问它是如何工作的。我不是问它是否会影响性能。我已经知道这些答案了。

我想知道 MS C# 团队最初将其添加到该语言中的动机是什么。你不会在语言中添加无聊的东西。它解决了一个值得注意的问题。那是什么问题?

我见过的“它解决的问题”最接近的例子是使用匿名类型时,如下所示:

var linqResult = from element in SomeCollection s
                 elect new { element.A, element.B } 

这种用法的讽刺之处在于,样式和编码标准指南(例如 Microsoft 提供的)建议编码器避免使用 ' var' 当结果类型不明显时。换句话说,“var”的(大概)预期目的与编码标准指南相冲突。

如果我正在编写编码标准,并试图防止过度使用“var”,我会有点倾向于说“仅在响应匿名类型时才使用“var””。但这又带来了一个完整的问题:将“var”添加到语言中的目的是什么?

Possible Duplicate:
What's the point of the var keyword?

I'm not asking how it works. I am not asking if it affects performance. I already know those answers.

I want to know what inspired the MS C# team to add it to the language in the first place. You don't add frivolous things to a language. There must have been a noteworthy problem it solved. What was/is that problem?

The closest example I've seen to "the problem it solves" is when using anonymous types, like this:

var linqResult = from element in SomeCollection s
                 elect new { element.A, element.B } 

The irony about this usage is that style and coding-standards guides (such as provided by Microsoft) advise the coder to avoid using 'var' when the resulting type is not obvious. In other words, the (presumably) intended purpose of 'var' is in conflict with the coding-standard guidelines.

If I were writing coding-standards, and was trying to prevent overuse of 'var', I'd be somewhat inclined to say "use 'var' only in response to anonymous types." But that brings the question full-circle: what was/is the purpose of having added 'var' to the language?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

戏蝶舞 2024-10-24 17:28:22

据我所知,var 关键字的创建很大程度上是为了通过启用匿名类型来使 LINQ 更容易

As far as I know the var keyword was made pretty much to make LINQ easierby enabling the use of anonymous types

枫以 2024-10-24 17:28:22

来自 MSDN

在很多情况下,var 的使用是
可选,只是一个语法
方便。然而,当一个变量
使用匿名类型初始化
您必须将变量声明为 var
如果您需要访问属性
稍后再查看该对象。这
是LINQ查询中常见的场景
表达式。

From MSDN:

In many cases the use of var is
optional and is just a syntactic
convenience. However, when a variable
is initialized with an anonymous type
you must declare the variable as var
if you need to access the properties
of the object at a later point. This
is a common scenario in LINQ query
expressions.

夏尔 2024-10-24 17:28:21

匿名类型是最重要的类型,但也减少了方法内的重复:

Dictionary<MyCustomType, List<MyOtherCustomType>> dict = new Dictionary<MyCustomType, List<MyOtherCustomType>>();

Anonymous Types was the big one, but also reducing repetition within methods:

Dictionary<MyCustomType, List<MyOtherCustomType>> dict = new Dictionary<MyCustomType, List<MyOtherCustomType>>();
成熟的代价 2024-10-24 17:28:21

完全正确!匿名类型。如果没有 var 并且仍然具有智能感知,那么您到底如何保留对创建的匿名类型的引用?

换句话说:当没有 var 时,就我们现在拥有的编译时支持而言,就不会有任何可用的匿名类型。匿名类型会出现太多运行时错误。

添加的糖分(过度使用时会变得复杂)是您甚至可以将 var 与非匿名变量一起使用。当然它有效,但这是一个坏习惯。

Exactly! Anonymous types. How in the world would you keep a reference to a created anonymous type if there was no var and still have intellisense along with it?

In other words: When there was no var, there wouldn't be any anonymous types in the usable sense we have now with compile time support. There would be just too many runtime errors with anonymous types.

The added sugar (of complexity when overused) is that you can use var with even non-anonymous variables. Sure it works but it's a bad habit.

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