Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

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.

License

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

Share

About the Author

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

Comments and Discussions

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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 20 Nov 2009
Article Copyright 2009 by Aviad P.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid