Click here to Skip to main content
Click here to Skip to main content

Can the C# ‘var’ keyword be misused?

By , 19 Nov 2009
 

I agree that in the above example, the use of var is a bit excessive. However, for very long types (such as a dictionary with the key and value both being lists of some nested classes... see below code example), this might actually improve readability (seeing so many details may overwhelm you). Assuming the method and variable are named well, you should have a general idea of what is returned. And, you can mouse over the "GetData" method to see what the return type is.

I'd say var in the example you give would be useful for long types but not very useful for short types. However, it wouldn't slow you down too much... if you really need to see what the type is, just mouse over the method.

// Readable, but must mouse over method to see type returned.
var x = GetPairs();
// Readable, but distracting.
Dictionary<List<Animal.Dog>, List<Animal.Cat>> y = GetPairs();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

AspDotNetDev
Web Developer
United States United States
Member
  • Managing Your JavaScript Library in ASP.NET (if you work with ASP.net and you don't read that, you are dead to me).
  • Graduated summa cum laude with a BS in Computer Science.
  • Wrote some articles and some tips.
  • DDR ("New high score? What does that mean? Did I break it?"), ping pong, and volleyball enthusiast.
  • Software I have donated to (you should too):

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralI recommend the use of an alias: using DogCat=Dictionary&lt...memberPIEBALDconsult4 Jan '12 - 8:27 
GeneralRe: Good advice. I often do this.protectorAspDotNetDev4 Jan '12 - 11:32 
GeneralReason for my vote of 5 it worksmemberarunkumar812715 Jul '11 - 2:12 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 19 Nov 2009
Article Copyright 2009 by AspDotNetDev
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid