为什么会无功是一件坏事? [英] Why would var be a bad thing?

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

问题描述

我一直在与同事聊天的一天,听说他们的编码标准明确禁止他们使用 VAR 关键字在C#。他们不知道为什么它是如此,我总是发现隐含的声明编码时要非常有用的。我从来没有任何问题,找出什么类型的变量是(你只悬停在VS变量,你会得到的类型的方式)。

I've been chatting with my colleagues the other day and heard that their coding standard explicitly forbids them to use the var keyword in C#. They had no idea why it was so and I've always found implicit declaration to be incredibly useful when coding. I've never had any problems finding out what type the variable was (you only hover over the variable in VS and you'll get the type that way).

有谁知道为什么它会是一个坏主意,使用var关键字在C#?

Does anyone know why it would be a bad idea to use the var keyword in C#?

推荐答案

框架设计指南的作者(真棒书),在2008年11月问世推荐使用 VAR 当Type是显而易见的,明确的考虑。

The writers of the .Net Framework Design Guidelines (awesome book) that came out in November 2008 recommend considering using var when the Type is obvious and unambiguous.

在另一方面,如果使用 VAR 将读取code时,安东Gogolev指出导致歧义,那么最好不要使用它

On the other hand, if using var would result in an ambiguity when reading the code, as Anton Gogolev pointed out, then it's better not to use it.

在书(附件一),他们其实举这个例子:

in the book (Annex A), they actually give this example:

var names = new List<string>(); // good usage of var

string source = GetSource();
var tokens = source.Split(' '); // ok; most developers know String.Split

var id = GetId(); // Probably not good; it's not clear what the type of id is

这是可能的,以确保可读性不会受到谦卑开发商的率性,您的组织已经决定,你是不值得 VAR ,并予以取缔。< BR>
这是一个耻辱,虽然,这就像在您的处置有一个很好的工具,但其保存在一个上锁的玻璃柜。

It's possible that, to ensure that readability is not subjected to the whims of lowly developers, your organisation has decided that you were not worthy of var and banned it.
It's a shame though, it's like having a nice tool at your disposal but keeping it in a locked glass cabinet.

在大多数情况下,使用 VAR 的简单类型实际上有助于可读性,我们决不能忘记,还有没有性能损失,使用 VAR

In most cases, using var for simple types actually helps readability and we must not forget that there is also no performance penalty for using var.

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

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