Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

Can the C# ‘var’ Keyword be Misused?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (14 votes)
19 Nov 2009CPOL 9.5K   1  
Can the C# ‘var’ keyword be misused?

Alternatives

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

Please Sign up or sign in to vote.
10 Jun 2011supercat9
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...
Please Sign up or sign in to vote.
10 Jul 2011Chris 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...
Please Sign up or sign in to vote.
25 Jul 2011Steve Wellens 6 alternatives  
Misuse of the C# var keyword.
Please Sign up or sign in to vote.
19 Nov 2009AspDotNetDev
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
Please Sign up or sign in to vote.
12 Jul 2011Quirkafleeg
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...
Please Sign up or sign in to vote.
10 Jun 2011AdrianRivera
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)


Written By
Software Developer (Senior)
Israel Israel
Software developer since 1984, currently specializing in C# and .NET.

Comments and Discussions