Using var
instead of explicit type makes refactorings much easier (therefore I must contradict the previous posters who meant it made no difference or it was purely "syntactic sugar").
You can change the return type of your methods without changing every file where this method is called. Imagine
...
List SomeMethod() { ... }
...
which is used like
...
IList list = obj.SomeMethod();
foreach (MyClass c in list)
System.Console.WriteLine(c.ToString());
...
If you wanted to refactor SomeMethod()
to return an IEnumerable
, you would have to change the variable declaration (also inside the foreach
) in every place you used the method.
If you write
...
var list = obj.SomeMethod();
foreach (var element in list)
System.Console.WriteLine(element.ToString());
...
instead, you don't have to change it.