What to use: var or object name type? [duplicate]

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.

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

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

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.

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.

Edit:

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.

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;

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.

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)

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.

Leave a Comment