用什么:VAR或对象名称类型?对象、名称、类型、VAR

2023-09-07 01:25:53 作者:风雅酒客

这是一个问题,当编程,我总是在想:用什么,当我们书面方式code:

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

var myFiles = Directory.GetFiles(fullPath);

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

无功的是新的,是的隐式类型的局部变量的,所以我们只能在本地使用,它有一个像不能规则空,等等,但我不知道如果我们使用它正常的任何优势。

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

在正常的部分说,不是在匿名类型 对象和集合初始化和查询前pressions 那是什么地方要使用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 :)

你有什么想法?

推荐答案

除了明显的使用 VAR 使用LINQ,我还用它来简化毛茸茸的变量声明为便于阅读, ,如:

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>>>();

在一般情况下,我得到了一种安慰(因缺乏一个更好的词)的静态类型,这让我舍不得放弃。我想,我知道当我声明一个变量我在做什么的感觉。声明一个变量是不是只是告诉编译器的东西,它说的是人阅读你的code的东西。

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.

让我给你举个例子。假设我有一个返回列表℃的方法;串&GT; 。这code无疑是正确的,我认为这是多么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

的确如此。但是的这个的版本的code不只是声明一个变量,它告诉我什么,是谁写的,它正试图做的人:

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();

谁写的code告诉我开发商:我不打算改变这个名单,我也没有打算使用索引来访问它的成员,而我要做的就是迭代跨越它。这是一个很大的信息传达在code一行。这是你放弃的东西,如果你使用 VAR

当然,你不能,如果你不使用它摆在首位放弃它。如果你是那种开发商谁也写一行code,你已经知道你不会用 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.

编辑:

我只是重读乔恩斯基特的帖子,这引自埃里克利珀跳出我:

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;

当我读到code,的事情,我想,当我读它是行之一的IEnumerable&LT; DataRow的&GT; 。因为我知道,什么LINQ查询返回的的IEnumerable&LT; T&GT; ,我可以看到对象的类型选择正确的有

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.

现在,在约90%,我使用LINQ的情况下,这不要紧,一小点点。由于90%的时间,code下一行是:

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)

但它不是很难想象code,其中这将是声明的行非常有用的IEnumerable&LT; D​​ataRow的&GT; - code其中有很多不同类型的对象了正在查询,这不是把查询声明旁边的迭代是可行的,并能够检查将是有益的与智能感知。这是一个什么样的东西,而不是怎么回事。

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.