Hello Nicholas,
This is important for anonymous types, when you don't know the type on the
right hand side (we know what it is like, but we dont know the specific
type). For instances where you know the type, I believe that you
shouldn't use var. In the OP's original post, where you are returning
IEnumerable<int>, I believe that that type should be used (as it is known)
and not var.
Well, I don't agree. I totally see your point, and I agree, that var
shouldn't be used all the time, and certainly not in the infamous "var i =
0;" case. But I see no harm, and in fact considerable benefit, in a
statement like this:
var myDict = new Dictionary<List<IList<List<int>>>, List<string>>();
Second, in a statement using a LINQ query expression, the type is not
obvious, it's not even there at all in the code. I know it's not entirely
the same thing, but I would also liken the implicit mention of the return
type to relying on an implementation detail. In any case, while I would
expect a good programmer who's familiar with LINQ to know what the return
type of such an expression is, I find the use of "var" with LINQ
expressions entirely acceptable and very useful, even if anonymous types
are not used.
The argument has been made that you can use var for types with large names
(such as large generic types, e.g. Dictionary<List<IList<List<int>>>,
List<string>>). However, I STRONGLY disagree with this position, as there
is already a mechanism in the language to aid with type aliasing, which is
the using declaration.
I already said above that my point of view is apparently different, but I
just want to mention that I don't understand what "the using declaration"
has to do with "types with large names", as you call them.
Oliver Sturm