使用什么:var 或对象名称类型? [英] What to use: var or object name type?

查看:24
本文介绍了使用什么:var 或对象名称类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个在编程时我一直想知道的问题:我们在编写代码时使用什么:

this is a question that when programming I always wonder: What to use when we are writing code:

var myFiles = Directory.GetFiles(fullPath);

string[] myFiles = Directory.GetFiles(fullPath);

var 是新的,是一个隐式类型的局部变量,所以我们只能在本地使用,它有一些规则,比如不能null 等,但我想知道我们是否可以正常"使用它.

var is new and is a Implicitly Typed Local Variables, so we can only use locally and it has rules like can't be null, etc., but I wonder if we get any advantage of using it "normally".

通常"部分表示,不是在 匿名类型对象和集合初始化器查询表达式 中的意图使用var 匿名对象,所以我的意思是......就像上面的例子一样.

The "normally" part says, not in Anonymous Types, Object and Collection Initializers and Query Expressions where that was the intent to use the var anonymous object, so what I mean is... just like the example above.

你有什么想法?

推荐答案

除了在 LINQ 中明显使用 var 之外,我还使用它来缩写多毛变量声明以提高可读性,例如:

Beyond the obvious use of var with LINQ, I also use it to abbreviate hairy variable declarations for readability, e.g.:

var d = new Dictionary<string, Dictionary<string, Queue<SomeClass>>>();

总的来说,我从静态打字中得到了一种安慰(因为想要一个更好的词),这让我不愿意放弃.我喜欢在声明变量时我知道自己在做什么的感觉.声明一个变量不仅仅是告诉编译器一些东西,它告诉阅读你代码的人一些东西.

In general, I get a kind of comfort (for want of a better word) from static typing that makes me reluctant to give it up. I like the feeling that I know what I'm doing when I'm declaring a variable. Declaring a variable isn't just telling the compiler something, it's telling the person reading your code something.

让我举个例子.假设我有一个返回 List 的方法.这段代码当然是正确的,我认为 90% 的 C# 开发人员可能会这样写:

Let me give you an example. Suppose I have a method that returns a List<string>. This code is certainly correct, and I think it's how 90% of C# developers would probably write it:

List<string> list = MyMethod();

显然,对吧?事实上,这里有一个您可以轻松使用 var 的地方.

Obviously, right? In fact, here's a place you could just as easily use var.

确实如此.但是这个版本的代码不仅仅是声明一个变量,它还告诉我编写它的人打算做什么:

True enough. But this version of the code isn't just declaring a variable, it's telling me what the person who wrote it is intending to do:

IEnumerable<string> list = MyMethod();

编写该代码的开发人员告诉我我不会更改此列表,也不会使用索引来访问其成员.我要做的就是遍历它."一行代码就包含了大量信息.如果你使用 var,这是你放弃的东西.

The developer who wrote that code is telling me "I'm not going to be changing this list, nor am I going to use an index to access its members. All I'm going to do is iterate across it." That's a lot of information to get across in a single line of code. It's something you give up if you use var.

当然,如果您一开始没有使用它,您就不会放弃它.如果您是那种会编写该行代码的开发人员,那么您已经知道您不会在那里使用 var.

Of course, you're not giving it up if you weren't using it in the first place. If you're the kind of developer who would write that line of code, you already know that you wouldn't use var there.

我刚刚重读了 Jon Skeet 的帖子,Eric Lippert 的这句话让我印象深刻:

I just reread Jon Skeet's post, and this quote from Eric Lippert jumped out at me:

隐式键入的本地变量只是您可以淡化方式从而强调内容的一种小方法.

Implicitly typed locals are just one small way in which you can deemphasize the how and thereby emphasize the what.

我认为实际上在很多情况下使用隐式类型是隐式的.不纠结于什么是可以的.例如,我会随便写一个 LINQ 查询,如:

I think that actually in a lot of cases using implicit typing is leaving the what implicit. It's just OK to not dwell on the what. For instance, I'll casually write a LINQ query like:

var rows = from DataRow r in parentRow.GetChildRows(myRelation)
           where r.Field<bool>("Flag")
           orderby r.Field<int>("SortKey")
           select r;

当我阅读该代码时,我想到的一件事是rows 是一个 IEnumerable."因为我知道 LINQ 查询返回的是 IEnumerable,并且我可以在那里看到被选择的对象的类型.

When I read that code, one of the things I think when I'm reading it is "rows is an IEnumerable<DataRow>." Because I know that what LINQ queries return is IEnumerable<T>, and I can see the type of the object being selected right there.

在这种情况下,没有明确说明了什么.由我来推断.

That's a case where the what hasn't been made explicit. It's been left for me to infer.

现在,在我使用 LINQ 的情况下,大约 90% 的情况下,这一点都不重要.因为 90% 的时间,下一行代码是:

Now, in about 90% of the cases where I use LINQ, this doesn't matter one tiny little bit. Because 90% of the time, the next line of code is:

foreach (DataRow r in rows)

但是不难想象代码中将 rows 声明为 IEnumerable 会非常有用 - 代码中有很多不同类型的对象被查询时,将查询声明放在迭代旁边是不可行的,并且能够使用 IntelliSense 检查 rows 会很有用.这是什么事情,而不是如何事情.

But it's not hard to envision code in which it would be very useful to declare rows as IEnumerable<DataRow> - code where a lot of different kinds of objects were being queried, it wasn't feasible to put the query declaration next to the iteration, and it would be useful to be able inspect rows with IntelliSense. And that's a what thing, not a how thing.

这篇关于使用什么:var 或对象名称类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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