Possible Duplicate:
What's the point of the var keyword?
Why does c# need the "var" identifier for type inferred type variables?
I mean what is the problem with just leaving it off:
a = 1;
//vs
var a = 1;
Reading Programming in Scala:
"Type variable" syntax you cannot simply leave off the type - there would be no marker to start the definition anymore.
But what is the different between leaving it off in the a:Int or int a?
At least one reason that comes to mind is that it forces the programmer to declare their intent to introduce a new variable, as opposed to assigning to an existing variable. This enables the compiler to detect numerous common coding errors.
Consider this:
class Foo
{
int a;
void Bar()
{
var a = 1;
}
}
Without the var
keyword, the assignment would be to the class member a
.
var
introduces unambiguously a new local variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With