Skip navigation.

Free Microsoft AJAX Templates for CodeSmith + Overview VideoAll recent postsLINQ Standard Query Operators Cheat Sheet

Monopoly: C# 3.0 Edition

Coming to a Toys R Us near you:

C# 3.0 community cheat: the var keyword

Comments

Comment permalink 1 Josh Stodola |
LOL great analogy!
Comment permalink 2 Jon |
I was just reading the spec for this ... I'm unable to come up with a situation where someone would use 'var' instead of explicitly defining the type. Can you give a real-world example of where it might be useful, by chance?
Comment permalink 3 Milan Negovan |
I drew these cards in good humor :)

On a more serious note, the 'var' keyword does have valid uses. Anonymous types are one such example. The whole idea is to not declare a class upfront, but have the compiler infer its structure. My worry is that people will be tempted to use 'var' haphazardly with trivial cases. Hence the 'get out of jail free' analogy.
Comment permalink 4 Daniel Steigerwald |
Keyword var is absolutely useful. Try to get type name of anonymous type :)

var someTemporallyDataShapingObject = new {
page.ID, page.InMenu, page.Order, page.ParentID, page.PublishedPage, page.Site, page.SiteID, page.State, page.Title, page.UrlTitle
}; // get only properties I need e.g. for JSON serialization.

Without var keyword, how could you reference anonymous type?
And, after one year of programming in javascript, I absolutely prefer
narrative code, than old C# bloated, a lot of indended and chatty code which do almost nothing, but spans over half screen ;)

Many new C# 3.0 construct allow you to write tight code.
Compare:
Dictionary>> result = new Dictionary>>();

with

var result = new Dictionary>>();

Why to insist seeing double ;) when its obvious what will be placed on left side :)
Comment permalink 5 Daniel Steigerwald |
Hi Milan, it seems your editor strips not only HTML tag :)
Comment permalink 6 Milan Negovan |
*cough* Yes, it thinks generics are HTML tags, too. ;)
Comment permalink 7 Vectorpedia |
I love your Get of Jail card........lol

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

TrackBacks

Sorry, TrackBacks are not allowed.

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):