Only use var as the result of a LINQ query, or if the type is very obvious from the same statement and using it would improve readability.
Wrong example:
var i = 3; // what type? int? uint? float?
var myfoo = MyFactoryMethod.Create("arg"); // Not obvious what base-class or
// interface to expect. Also difficult
// to refactor if you can't search for
// the class
Correct example:
var q = from order in orders where order.Items > 10 and order.TotalValue > 1000;
var repository = new RepositoryFactory.Get();
var list = new ReadOnlyCollection();
In all of three above examples it is clear what type to expect. For a more detailed rationale about the advantages and disadvantages of using var, read Eric Lippert's "Uses and misuses of implicit typing".
|