什么是“无功”的目的是什么? [英] What is the purpose of 'var'?

查看:157
本文介绍了什么是“无功”的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
什么是var关键字的地步?

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

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

我想知道是什么启发了MS C#的团队将其添加到语言的摆在首位。你不轻浮的东西添加到语言。一定有它解决了一个值得注意的问题。什么是/是这个问题。

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?

推荐答案

匿名类型是最大的一个,也是方法中减少重复:

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

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

这篇关于什么是“无功”的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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