Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't C# let you declare multiple variables using var?

Given the following:

// not a problem int i = 2, j = 3; 

so it surprises me that this:

// compiler error: Implicitly-typed local variables cannot have multiple declarators var i = 2, j = 3; 

doesn't compile. Maybe there is something I don't understand about this (which is why I'm asking this)?

But why wouldn't the compiler realize that I meant:

var i = 2; var j = 3; 

which WOULD compile.

like image 717
aarona Avatar asked Feb 09 '11 20:02

aarona


People also ask

Why there is no string in C?

There is no string type in C . You have to use char arrays. By the way your code will not work ,because the size of the array should allow for the whole array to fit in plus one additional zero terminating character.

Why C has no exception handling?

As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno.

Why is C difficult?

It is hard to learn because: It has complex syntax to support versatility. It is a permissive language—you can do everything that's technically possible, even if not logically right. It is best learned by someone who already has a foundation with C programming.


2 Answers

When we designed the feature I asked the community what

var x = 1, y = 1.2; 

should mean. The question and answers are here:

http://blogs.msdn.com/b/ericlippert/archive/2006/06/26/what-are-the-semantics-of-multiple-implicitly-typed-declarations-part-one.aspx

http://blogs.msdn.com/b/ericlippert/archive/2006/06/27/what-are-the-semantics-of-multiple-implicitly-typed-declarations-part-two.aspx

Briefly, about half the respondants said that the obviously correct thing to do was to make x and y both double, and about half the respondants said that the obviously correct thing to do was to make x int and y double.

(The language committee specified that it should be "double", and I actually implemented the code that way long before we shipped. We used the same type inference algorithm as we do for implicitly typed arrays, where all the expressions must be convertible to a best element type.)

When half your customer base thinks that one thing is "obviously correct" and the other half believes that the opposite is "obviously correct" then you have a big design problem on your hands. The solution was to make the whole thing illegal and avoid the problem.

like image 187
Eric Lippert Avatar answered Oct 16 '22 21:10

Eric Lippert


It's just another point of possible confusion for the programmer and the compiler.

For example this is fine:

double i = 2, j = 3.4; 

but what does this mean?

var i = 2, j = 3.4; 

With syntactic sugar this kind of thing is a headache no one needs--so I doubt your case would ever be supported. It involves too much of the compiler trying to be a little bit too clever.

like image 24
James Gaunt Avatar answered Oct 16 '22 20:10

James Gaunt