I do not think that using var should be a problem - and I prefer it precisely for the purpose of reading code. First of all, var is only syntactic sugar and simply compiles to the proper type when IL is emitted. Regarding code readability, it makes sense to focus on the purpose for which the variable is used, and how it is assigned, and not just its type. The VS.NET editor shows the type in the line following it, anyway - if you just hover over it. So this should not be a problem. And as for debugging - if you see the Autos / Local / Watch windows - they display the types of all members.
I like to see code like this:
var customers = GetCustomerList(); foreach (var customer in customers) { customer.ProcessOrders(); }
Unlike
List<CustomerObjectDeserializedFromWebService> customers = GetCustomers(); foreach (CustomerObjectDeserializedFromWebService customer in customers) { customer.ProcessOrders(); }
var has its fairness, limited to using variables in local declarations that are also initialized during declaration. And in this case, if you omit the actual type, it definitely improves the readability of IMO.
EDIT: And for my part, it would be unfair not to warn about use, as shown below:
var x = 20;
This is not good; when a literal is applicable to several types, you need to know the default data type and therefore understand what is output for type x. Yes, by all means, I would avoid such statements.
source share