Jun 23 2008
C# 3.0 and Implicitly Typed Local Variables
In Jeff Atwoods Department of Declaration Redundancy post he talks about the benefits of the ability to implcitly set the type of local variables in C#. Whilst I don’t have a problem with being able to condense
BufferedReader br = new BufferedReader (new FileReader(name));
into
var br = new BufferedReader (new FileReader(name));
Personally, if you wanted to condense that code, I would be far happier for it to be condensed on the right hand side of the assignment rather than the left. For example:
BufferedReader br = new (new FileReader(name));
It’s still clear what the type of br is and you still get the benefits of reducing duplication. The other reason I like this is that it also leaves var to be used where it probably has the most benefit – when defining anonymous types. For example:
var obj = {Prop1: "a", Prop2: 112};
I think what it boils down to is that var is being used to solve two problems. If the scope of var was left to anonymous types and the problem of duplcation in object declarations was solved implicitly through improved syntax it would provide a clearer direction on how each feature should be used. So often in the past features have been introduced with no clear scope as to the problem they are trying to solve and eventually they get used to solve problems they were never intended to solve.
I also feel that the following is just going too far:
var url = "http://tinyurl.com/5pfvvy";
var maxentries = 5;
var pi = 3.14159;
var n = new int[] {1, 2, 3};
The main problem I have with this code is that whilst it’s easy to see that url is a string, you can’t tell the type of pi or maxentries. The assumption would be that maxentries is of type int, but there’s no way to be certain without calling GetType. It’s even less obvious with pi. Is it a decimal, double or float?
At the end of the day, I guess it all comes down to personal preference but you should always research and carfully consider the use of newly introduced features into any product you use.

With these things in mind, I’ve been using Firefox 3 for the last few days since it’s official release and I have to say, I’m impressed. I’ve used FF2 and Safari in the past, but on my windows machine, I’ve always found myself coming back to IE7.