Click here to Skip to main content
13,896,567 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 19 Nov 2009
Licenced CPOL

Can the C# ‘var’ keyword be misused?

, 25 Jul 2011
Rate this:
Please Sign up or sign in to vote.
Misuse of the C# var keyword.

More and more often I've been seeing C# code like this:

var Data = GetData();

What on earth does GetData() return? This code is not as maintainable as it could be and is not as maintainable as it should be.

Doesn't explicitly declaring the variable type make the code more readable, understandable, and ultimately more maintainable?

DataTable Data = GetData(); 

Ahhh, GetData() returns a DataTable.

I know that var has uses but I wish it would have been named something much longer because typing 'var' is too easy. Perhaps it should have been named AutomaticTypeVar or even AutoVar to reduce the lazy misuse.

Just my 2 cents.

A user on the Asp.Net forums was kind enough to provide this quote and link:

"However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required."[^]


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


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.

You may also be interested in...

Comments and Discussions

AnswerYes, it can... Pin
zyrconium137-Jan-13 20:06
memberzyrconium137-Jan-13 20:06 
GeneralRe: Q: How do you know "he" named the method GetData? A: You don... Pin
Adrian Cole17-Dec-11 20:22
memberAdrian Cole17-Dec-11 20:22 
GeneralRe: The name has nothing to do with it. The return type of GetDa... Pin
Heath Stewart10-Jul-11 17:02
protectorHeath Stewart10-Jul-11 17:02 
GeneralReason for my vote of 5 var was added to support Linq, where... Pin
PIEBALDconsult4-Jan-12 8:17
protectorPIEBALDconsult4-Jan-12 8:17 
GeneralRe: That's funny...but keep your day job. I'm not a big fan of ... Pin
Steve Wellens4-Jan-12 10:08
memberSteve Wellens4-Jan-12 10:08 
GeneralReason for my vote of 5 You are preaching to the choir here.... Pin
fjdiewornncalwe1-Nov-11 7:09
professionalfjdiewornncalwe1-Nov-11 7:09 
GeneralI still don't see what the big deal is. Wanna know what Get... Pin
spring19751-Aug-11 10:19
memberspring19751-Aug-11 10:19 
GeneralCore problem in "var" declarations is inability of someone w... Pin
Kelqualyn26-Jul-11 0:17
memberKelqualyn26-Jul-11 0:17 
GeneralReason for my vote of 5 I would totally agree with Steve: us... Pin
DrABELL25-Jul-11 17:37
professionalDrABELL25-Jul-11 17:37 
GeneralWe have a simple rule for the use of var: it may ONLY be use... Pin
chrisbray18-Jul-11 11:51
memberchrisbray18-Jul-11 11:51 
GeneralReason for my vote of 5 I couldn't agree more. Intellisense ... Pin
BloodyBaron18-Jul-11 11:01
memberBloodyBaron18-Jul-11 11:01 
GeneralI coulnd't agree more with you, Steve :p Pin
BloodyBaron18-Jul-11 10:59
memberBloodyBaron18-Jul-11 10:59 
GeneralI was being facetious. Hover your mouse over this: va... Pin
Steve Wellens17-Jun-11 2:33
memberSteve Wellens17-Jun-11 2:33 
General>> We got intellisense that can tell us >> how to handle th... Pin
Steve Wellens14-Jun-11 1:57
memberSteve Wellens14-Jun-11 1:57 
GeneralRe: Two ways IntelliSense can help you: * Hover over it with you... Pin
isaks17-Jun-11 2:30
memberisaks17-Jun-11 2:30 
GeneralRe: jgauffin is absolutely correct. The problem is not on the le... Pin
IAbstract18-Jul-11 11:03
memberIAbstract18-Jul-11 11:03 
GeneralRe: It's a poor programmer who writes such code. The var keyword... Pin
Dave The Brave21-Jul-11 4:21
memberDave The Brave21-Jul-11 4:21 
GeneralThe problem is not the "var" keyword, but that you have name... Pin
jgauffin13-Jun-11 20:37
memberjgauffin13-Jun-11 20:37 
GeneralRe: I concur, the problem is not the var keyword, the problem is... Pin
Dave The Brave21-Jul-11 4:20
memberDave The Brave21-Jul-11 4:20 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190306.1 | Last Updated 25 Jul 2011
Article Copyright 2009 by Steve Wellens
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid