Skip Navigation LinksHome > View Post

More on to var or not to var

Yesterday, I posted a poll asking for peoples opinions on the use of the var keyword with non anonymous types.

At the time of writing, the "No! It's just for anonymous types" have the vote with 51% against 44% - and 5% not really caring (although the vote might have been slightly skewed by some of my friends campaigning:).

I just found a post by Dare Obasanjo from earlier this year with the same title: C# 3.0 Implicit Type Declarations: To var or not to var.

In it, he cites several references on the matter including two posts by a member of the Resharper team called ReSharper vs C# 3.0 - Implicitly Typed Locals and Varification -- Using Implicitly Typed Locals and, perhaps most compelling, the C# Reference which states:

Overuse of var can make source code less readable for others. It is recommended to use var only when it is necessary, that is, when the variable will be used to store an anonymous type or a collection of anonymous types.

This closes the argument for Dare as he states "Case Closed" and it is tough to argue when the authors of the language make such a statement. Though a little part of me does wonder why the creators of C# didn't prevent this usage if they feel that way.

UPDATE: I found another piece of documentation on MSDN that says just the opposite, see Yet more on to var or not to var

I'll close the poll in a few days, so get your vote in now. Then I'll rerun it in about a years time and we'll see if opinions have changed.

Tags: C#

 
Josh Post By Josh Twist
3:31 AM
19 Jul 2008

» Next Post: Yet more on to var or not to var
« Previous Post: To var or not to var

Comments are closed for this post.

Posted by Michael Giagnocavo @ 19 Jul 2008 8:42 PM
"it is tough to argue when the authors of the language make such a statement"

C# didn't have generics until version 2, or succinct lambdas until version 3. So why is a line in the spec authoratative on good code?

Posted by Michael Giagnocavo @ 19 Jul 2008 9:03 PM
*authoritative, sigh.

© 2005 - 2014 Josh Twist - All Rights Reserved.