13

Possible Duplicate:
What's the point of the var keyword?

I'm not asking how it works. I am not asking if it affects performance. I already know those answers.

I want to know what inspired the MS C# team to add it to the language in the first place. You don't add frivolous things to a language. There must have been a noteworthy problem it solved. What was/is that problem?

The closest example I've seen to "the problem it solves" is when using anonymous types, like this:

var linqResult = from element in SomeCollection s
                 elect new { element.A, element.B } 

The irony about this usage is that style and coding-standards guides (such as provided by Microsoft) advise the coder to avoid using 'var' when the resulting type is not obvious. In other words, the (presumably) intended purpose of 'var' is in conflict with the coding-standard guidelines.

If I were writing coding-standards, and was trying to prevent overuse of 'var', I'd be somewhat inclined to say "use 'var' only in response to anonymous types." But that brings the question full-circle: what was/is the purpose of having added 'var' to the language?

Community
  • 1
  • 1
Brent Arias
  • 29,277
  • 40
  • 133
  • 234
  • you really answered your question. `var` was introduced to support anonymous types, because there is no other way to have variable of anonymous type. – Andrey Feb 16 '11 at 18:34
  • You already have the answer: the original purpose was to handle anonymous types. – Darin Dimitrov Feb 16 '11 at 18:34
  • @David - Bingo, I was looking for that. – ChaosPandion Feb 16 '11 at 18:35
  • Also: http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c – Amy B Feb 16 '11 at 18:35
  • Can anyone explain to me why `var` causes so much confusion among developers? – ChaosPandion Feb 16 '11 at 18:36
  • Honestly, ignore those guidelines. They are not particularly well thought-out. – Konrad Rudolph Feb 16 '11 at 18:37
  • @ChaosPandion: Those three letters arranged in such an order. Don't they look **menacing**? And they appear in so many languages and not just C#. They just keep haunting... and haunting... – BoltClock Feb 16 '11 at 18:38
  • The feature was specifically added to enable anonymous types. It doesn't conflict the guideline about using it for non-obvious types since an instance of an anonymous type will be visible in the initialization expression, as opposed to calling a method whose return type is not obvious. – Mark Cidade Feb 16 '11 at 18:39
  • @ChaosPandium Some people see "var" and think of PHP's dynamic typing and get scared. The name may not have been the best choice. – Michael Stum Feb 16 '11 at 18:42
  • Just think about how much better the world would be if "anon" was chosen instead of "var". – Amy B Feb 16 '11 at 18:45
  • 1
    @David - Or possibly `infer` because some of crazy programmers use `var` for more than just anonymous types. – ChaosPandion Feb 16 '11 at 18:58

4 Answers4

15

Anonymous Types was the big one, but also reducing repetition within methods:

Dictionary<MyCustomType, List<MyOtherCustomType>> dict = new Dictionary<MyCustomType, List<MyOtherCustomType>>();
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
  • 1
    Yes. This is the case when **it's obvious** what type the variable is. Hence it can be used in this scenario (even though I don't). – Robert Koritnik Feb 16 '11 at 18:38
  • @Robert That's a stylistic decision and an option. For Repetitions I always use it (type x = new type()), but Method Calls it's a case by case decision. – Michael Stum Feb 16 '11 at 18:41
9

Exactly! Anonymous types. How in the world would you keep a reference to a created anonymous type if there was no var and still have intellisense along with it?

In other words: When there was no var, there wouldn't be any anonymous types in the usable sense we have now with compile time support. There would be just too many runtime errors with anonymous types.

The added sugar (of complexity when overused) is that you can use var with even non-anonymous variables. Sure it works but it's a bad habit.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • 1
    You'd use an object, but the downside is you wouldn't have intellisense. – Yuriy Faktorovich Feb 16 '11 at 18:35
  • @Yuriy: You wouldn't only not have Intellisense, but also no Properties - the anonymous type in the OP example has two properties: .A and .B. You can't access System.Object.A since Object does not have this property, so you need to cast it. Cast it to what though? In .net 4, dynamic could solve that, but .net 3 didn't have that. – Michael Stum Feb 16 '11 at 18:38
  • 1
    @Michael: sure I could, through reflection. I just really wouldn't want to. – Yuriy Faktorovich Feb 16 '11 at 18:39
1

As far as I know the var keyword was made pretty much to make LINQ easierby enabling the use of anonymous types

Francisco Noriega
  • 13,725
  • 11
  • 47
  • 72
0

From MSDN:

In many cases the use of var is optional and is just a syntactic convenience. However, when a variable is initialized with an anonymous type you must declare the variable as var if you need to access the properties of the object at a later point. This is a common scenario in LINQ query expressions.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222