Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

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   2
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:

C#
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.

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

 
GeneralMy 5 Pin
Andreas Gieriet4-Sep-12 7:00
professionalAndreas Gieriet4-Sep-12 7:00 
GeneralReason for my vote of 5 Yeah, like Uncle Bob said (Clean Cod... Pin
HMIWalter16-Jun-11 3:08
HMIWalter16-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.