Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Alternatives
 
Alternatives (6)

Tagged as

Can the C# ‘var’ keyword be misused?

, 25 Jul 2011 CPOL
Misuse of the C# var keyword.

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

19 Nov 2009
Aviad P.
Can the C# ‘var’ keyword be misused?
10 Jun 2011
supercat9
If the right-hand operand of an assignment is a "new", there's no need to explicitly specify the type of the new variable being created unless the new variable will have to hold items of a more general type than the right-hand operand would indicate. I would consider the code clearer without the...
10 Jul 2011
Chris Randle
I've always used var because ReSharper suggested it by default. However, like most people, I only did this when I didn't understand the conotations of ReSharper's suggestion. Now I disable this suggestion in the plug-in, and in actual fact, I prefer to use the fully qualified name on at least...
19 Nov 2009
AspDotNetDev
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). Assu
12 Jul 2011
Quirkafleeg
The original question was whether this practice can lead to misuse. Of course it can - almost everytime it is used! In the "real world" (business), most programmers spend their days working amongst reasonably old code with several bolt-on fixes. It becomes increasingly hard to maintain because...
10 Jun 2011
AdrianRivera
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...

License

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

Share

About the Author

Steve Wellens
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

| | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 25 Jul 2011
Article Copyright 2009 by Steve Wellens
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid