Click here to Skip to main content
13,549,148 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 19 Nov 2009
Licenced CPOL

Can the C# ‘var’ Keyword be Misused?

, 19 Nov 2009
Rate this:
Please Sign up or sign in to vote.
Can the C# ‘var’ keyword be misused?

It can be misused of course, if you can't figure out by glancing at the code, what the code does. In your example, for one, it is bad, but then again, GetData() is bad too, what does the function do? If the function had a meaningful name, then maybe the var declaration would actually be good in clearing confusion:

var productsTable = GetProducsDataTable();

In my opinion, the var here is excellent, it doesn't confuse the reader with unnecessary details like what the datatype is exactly (a dictionary, a datatable, etc.) but it does tell him what it semantically contains, which is probably what he needs to know. If he does need to know the type, he can use the various ways mentioned here, like mousing over the method, or jumping to its declaration, or mousing over the productsTable variable.


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


About the Author

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

You may also be interested in...

Comments and Discussions

GeneralMy 5 Pin
Andreas Gieriet4-Sep-12 7:00
memberAndreas Gieriet4-Sep-12 7:00 
GeneralReason for my vote of 5 Yeah, like Uncle Bob said (Clean Cod... Pin
HMIWalter16-Jun-11 3:08
memberHMIWalter16-Jun-11 3:08 

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 | Terms of Use | Mobile
Web01 | 2.8.180515.1 | Last Updated 20 Nov 2009
Article Copyright 2009 by Aviad P.
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid