I haven’t really used C# since version 2.0. So while browsing Stack Overflow and other programming sites, I realize that I need to catch up on the new, seemingly useful and really interesting features that have been added to C# since (Such as LINQ, which will be the subject of a different post). Version 3.0 introduced anonymous types and consequently, local variable type inference. This basically means that it is now possible to this:
// before, you would do this
string name = "John Doe"
int age = 19;
// now there is local variable type inference
var name = "John Doe"
var age = 19;
// the following is an example of anonymous types
var anon = new { Age = 19, Country = "Brazil" };
So it is no longer necessary to explicitly declare a variable’s type. There is a good article on this at MSDN on Implicitly Typed Local Variables that is worth a read.
The question is, when should one use the var keyword? Take a look at this Stack Overflow question: What’s the point of the var keyword? In it, people note that using var can actually save you some time and in some cases make code a lot more readable. As the MSDN article says, it is important to note that var does not stand for “variant”, so it does not mean that the variable is loosely-typed as you find in certain languages such as Python. Instead, the compiler determines, or infers, the type based on the right hand side expression.
This can be helpful in case you have a complicated initialization statement such as:
Dot<SomeType<AnotherType>> thing = new Dot<SomeType<AnotherType>>();
This is redundant and can become difficult to read, as well as being more error-prone. There’s a higher risk of typos for example, or misinterpretation. Instead, the use of the var keyword can alleviate these problems, allowing you to instead do:
var dot = new Dot<SomeType<AnotherType>>();
So when would it not be encouraged to use var? Most, as noted in the question: Why would var be a bad thing?, note that it might not be a good idea to use it when the inferred type is ambiguous. For example, a line like this:
var wtf = GetSomething();
It isn’t all that obvious what GetSomething returns, assuming that Something isn’t an actual class. Even if you wrote that function and you know exactly what it does, it probably wouldn’t be all that readable to someone coming in and looking at your code. This problem might be easily fixed with a comment, but I agree with the idea of explicitly defining the type in ambiguous cases, so I would make it:
RoflCopter choppa = GetToDaChoppa();
Here we know that GetToDaChoppa returns a RoflCopter object. For a more elaborate explanation, check out this answer on that same question.
My Personal Opinion
I will personally use var only when I believe it will ease readability by reducing redundancy, or when I require the use of anonymous types. Another situation I might use it in is in conjunction with LINQ, in cases where the type doesn’t really matter, as long as you know you’re getting an IEnumerable. I’m not sure how I feel about mixing explicit type definitions with the implicit that is var. I might end up liking it, but as of now the over-use of var just seems pretty lazy and messy to me. I have a feeling it might confuse beginners and they’ll imagine that they should just use var wherever possible to avoid having to learn about type definitions, which is why I believe it might be a lazy habit.
But there are some people that really do love it, apparently. Like I said, I don’t hate the idea or anything, it’s just new to me so I guess it’s something that will take time getting used to. I’m usually really open to changes. I learned and loved Objective-C, where most whine at the first sign of something breaking convention. But I’m pretty sure I’ll eventually get used to it and possibly even like it. In fact, the next revision of C++ will also have Type Inference with the auto keyword.
For most part I agree, I wouldn’t say that the way it removes redundancy that it improves readability, because it removes it on the wrong side if the “=”…
In most cases for the above, my eyes don’t even “see” the second declaration, it basically just reads to me as:
If we were ever to remove redundancy, this would also be the way I would wan’t it for the simple fact that we read from left to right…
But for the rest… I agree, sadly the world has adopted the sense of “var everywhere”, and whenever I now download an open source project, it becomes a PAIN to read… it simply looks “Infected” to me…