Personally I only use var in two places:
- With anonymous types, ie. LINQ-related (where var is required in some cases)
- When the statement declares and constructs a specific type at the same type
ie. this is an example of point 2:
var names = new List<String>();
Edited: This in response to Jon Skeet's question.
The above answer was in fact simplified. Basically, I use var where the type is either:
- Unnecessary to know (not that many places though)
- Impossible to know (LINQ, anonymous types)
- Otherwise known, or clear from the code
In the case of a factory method, where all you need to know at the place where you write the code is that the object you get back is a descendant of some type, and that some type has a static factory method, then I would use var. Like this:
var connection = DatabaseConnection.CreateFromConnectionString("...");
The above example is a real example from my code. It is clear, at least to me and the people that use this code, that connection is a DatabaseConnection descendant, but the exact type is not needed for neither understanding the code, nor using it.