Possible Duplicate:
Advantage of var keyword in C# 3.0
yesterday I stumbled upon a recomendation from MS, that I should use var, when ever possible:
http://msdn.microsoft.com/en-us/library/ff926074.aspx
I always thought that using the correct Type would help documenting the code and also help findig bugs at compile time.
What is the reason behind this recomendation?
Best Thomas
Using implicit typing does NOT mean that the variable is not strongly-typed. It means that the compiler implies the type from the right-hand side of the statement.
var i = 1;
i
is defines as having type int
. It's exactly the same as saying int i = 1;
but the type is implied.
Similarly, the following code is a lot easier to read:
var pairs = new List<pair<int, IEnumerable<string>>>();
than if you had to type:
List<pair<int, IEnumerable<string>>> pairs = new List<pair<int, IEnumerable<string>>>();
and yet the results are exactly the same.