Besides the obvious use of var with LINQ, I also use it to shorten declarations of hairy variables for readability, for example:
var d = new Dictionary<string, Dictionary<string, Queue<SomeClass>>>();
In general, I get some comfort (due to the lack of a better word) from static text input, which makes me reluctant to abandon it. I like the feeling that I know what I do when I declare a variable. Declaring a variable doesn't just tell the compiler something that tells the person something reads your code.
Let me give you an example. Suppose I have a method that returns a List<string> . This code is certainly correct, and I think that 90% of C # developers will probably write it:
List<string> list = MyMethod();
Obviously right? In fact, here you could easily use var .
Right. But this version of the code does not just declare a variable, it tells me what the one who wrote it intends to do:
IEnumerable<string> list = MyMethod();
The developer who wrote this code tells me: "I'm not going to change this list, and I will not use the index to access its members. All I am going to do is iterate over it." This is a lot of information that can be found in one line of code. This is what you will refuse if you use var .
Of course, you will not refuse if you do not use it in the first place. If you are such a developer who would write this line of code, you already know that you will not use var there.
Edit:
I just re-read John Skeet's post, and this quote from Eric Lippert popped up on me:
Implicitly printed locales are just one small way in which you can defocus how to emphasize that.
I think that in many cases the use of implicit typing leaves the implicit. This is normal so as not to stop at anything. For example, I accidentally write a LINQ query, for example:
var rows = from DataRow r in parentRow.GetChildRows(myRelation) where r.Field<bool>("Flag") orderby r.Field<int>("SortKey") select r;
When I read this code, one of the things that I think when I read is " rows is IEnumerable<DataRow> ". Since I know that LINQ queries are returned, this is an IEnumerable<T> , and I can see the type of the selected object.
This is the case when that which has not been made explicit. I could only conclude.
Now in about 90% of cases when I use LINQ, it does not matter, one small bit. Since 90% of the time, the following line of code:
foreach (DataRow r in rows)
But it’s easy to imagine code in which it would be very useful to declare rows as IEnumerable<DataRow> - a code in which many different types of objects were requested, it was impossible to place a query declaration next to iteration, and it would be useful to be able to check rows for IntelliSense. And this is something, not how.