在C#中使用var关键字的

在C#中使用var关键字的

问题描述:

提供有关在C#中使用'变种'关键字的同事讨论后3我想知道是什么人的意见通过VAR是类型推断的适当使用?

After discussion with colleagues regarding the use of the 'var' keyword in C# 3 I wondered what people's opinions were on the appropriate uses of type inference via var?

例如我可疑的情况下,而懒洋洋地使用VAR,例如: -

For example I rather lazily used var in questionable circumstances, e.g.:-

foreach(var item in someList) { // ... } // Type of 'item' not clear.
var something = someObject.SomeProperty; // Type of 'something' not clear.
var something = someMethod(); // Type of 'something' not clear.

是变种的

更多合法的用途如下: -

More legitimate uses of var are as follows:-

var l = new List<string>(); // Obvious what l will be.
var s = new SomeClass(); // Obvious what s will be.

有趣的LINQ似乎有点灰色地带,例如组成: -

Interestingly LINQ seems to be a bit of a grey area, e.g.:-

var results = from r in dataContext.SomeTable
              select r; // Not *entirely clear* what results will be here.

很清楚什么结果将在这将是其实现IEnumerable一种类型,但它是不以同样的方式一个变种声明一个新的对象完全是显而易见的。

It's clear what results will be in that it will be a type which implements IEnumerable, however it isn't entirely obvious in the same way a var declaring a new object is.

这是当它涉及到LINQ的对象,例如更糟糕: -

It's even worse when it comes to LINQ to objects, e.g.:-

var results = from item in someList
              where item != 3
              select item;

这是不超过equivilent的foreach(在someList VAR项){// ...} equivilent。

This is no better than the equivilent foreach(var item in someList) { // ... } equivilent.

有大约类型安全一个真正的问题在这里 - 例如,如果我们到查询的结果放到一个重载的方法接受IEnumerable的&LT; INT&GT;和IEnumerable&LT;双&GT;主叫方可能会在无意中传递了错误的类型。

There is a real concern about type safety here - for example if we were to place the results of that query into an overloaded method that accepted IEnumerable<int> and IEnumerable<double> the caller might inadvertently pass in the wrong type.

VAR 确实的保持强类型,但真正的问题是它是否是危险的类型不会对定义立即显现出来,一些东西,当过载的意思是当你无意中传递了错误类型的方法编译器错误可能不会发出放大。

var does maintain strong typing but the question is really whether it's dangerous for the type to not be immediately apparent on definition, something which is magnified when overloads mean compiler errors might not be issued when you unintentionally pass the wrong type to a method.

我仍然认为 VAR 可以使code在某些情况下更具有可读性。如果我有与订单属性Customer类,我想赋值给一个变量,我只是这样做:

I still think var can make code more readable in some cases. If I have a Customer class with an Orders property, and I want to assign that to a variable, I will just do this:

var orders = cust.Orders;

我不在乎,如果Customer.Orders是的IEnumerable&LT;排序&gt; 的ObservableCollection&LT;排序&gt; 或的BindingList&LT;排序&gt; - 我要的是保持该列表在内存中遍历它,或者得到其计数或更高版本上的东西。

I don't care if Customer.Orders is IEnumerable<Order>, ObservableCollection<Order> or BindingList<Order> - all I want is to keep that list in memory to iterate over it or get its count or something later on.

通过对比上述声明:

ObservableCollection<Order> orders = cust.Orders;

对我来说,类型名称只是噪音。如果我回去,并决定改变Customer.Orders类型走下赛场(比如从的ObservableCollection&LT;排序&gt; 的IList&LT;排序&gt; ),那么我需要改变太多的声明 - 这是我不会做,如果我想在第一时间使用VAR

To me, the type name is just noise. And if I go back and decide to change the type of the Customer.Orders down the track (say from ObservableCollection<Order> to IList<Order>) then I need to change that declaration too - something I wouldn't have to do if I'd used var in the first place.