TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C# Coding Standard
Search

Rule:  10@501

Synopsis:Only use var when the type is very obvious
Language:C#
Severity Level:3
Category:Data types


Description:

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".



Literature References:
Aviva AV1520