Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Can the C# ‘var’ keyword be misused?

0.00/5 (No votes)
10 Jun 2011 2  
I think this is a nice future, for example look this code:Dictionary > myDic = new Dictionary >();We really need to tell the compiler in this expression what the type of myDic is?This is more concise and clear at...
I think this is a nice future, for example look this code:

Dictionary< SomeClass, List < SomeOtherClass > > myDic = new Dictionary< SomeClass, List < SomeOtherClass > >();


We really need to tell the compiler in this expression what the type of myDic is?

This is more concise and clear at least to me:

var myDic = new Dictionary< SomeClass, List < SomeOtherClass > >();


If you are developing an application, you should know the semantic of the method, then the code
var productsTable = GetProducsDataTable();
should not be a problem, if you are just reading the code, check this out.

List<product> products = GetData();
foreach(var product in products)
// doSomethingWithProduct(product)
</product>


The no less readable code:
var products = GetData();
foreach(var product in products)
// doSomethingWithProduct(product)


I think this is dependant on the context. I'm completely sure that if you choose your methods and variables names well, the code written will be more readable.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here