Click here to Skip to main content
12,243,192 members (34,313 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3K views
1 bookmarked
Posted

Can the C# ‘var’ keyword be misused?

, 10 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

AdrianRivera
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
General[My vote of 1] Your opinion is unclear Pin
PIEBALDconsult6-May-12 17:19
memberPIEBALDconsult6-May-12 17:19 
GeneralReason for my vote of 5 the type is defined 2 times in the l... Pin
fgrimm20-Oct-11 22:02
memberfgrimm20-Oct-11 22:02 
GeneralReason for my vote of 1 good Pin
arunkumar812715-Jul-11 3:11
memberarunkumar812715-Jul-11 3:11 
GeneralReason for my vote of 1 I completely disagree. The var keywo... Pin
Dave The Brave15-Jul-11 1:43
memberDave The Brave15-Jul-11 1:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 10 Jun 2011
Article Copyright 2011 by AdrianRivera
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid