Why does c# need "var" identifier? [duplicate]
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 t开发者_如何学Cype - 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.
You have to define a variable before you use in C#. Sure, the compiler could detect when it reaches
a = 1;
That a had not yet been defined and so define the variable and assign it the value of 1. However, it could lead to other issues where you have:
MyOwnClass myVeryLongVariableNameThatIsUsedAllOverThePlace = new MyOwnClass();
myveryLongVariableNameThatIsUsedAllOverThePlace = input.GetNewClass();
Now you have 2 variables, where you thought you had one.
From MSDN:
It is important to understand that the var keyword does not mean "variant" and does not indicate that the variable is loosely typed, or late-bound. It just means that the compiler determines and assigns the most appropriate type.
The idea is to keep the robustness of C# by preventing accidental Implicitly Typed Local Variable.
Technically, there's really no need. Other languages allow you to combine declaration and assignment with no special keywords.
If a = 1
was valid syntax for both assignment and declaration, though, code could become very confusing which is, I believe, why C# requires at least var
.
keyword will tell the compiler not to try resolve the identifier
outside the scope of the method.
Can C# work without it? Yes, but this enforces a discipline which is required for a strongly typed language.
Different programming languages are designed to suit different purposes. C# is designed for writing large-scale high-reliability software. Part of its approach to this is using explicitly-declared staticly-typed variables. The ability to infer variable types was required by the addition of anonymous objects, which were required for LINQ query expressions. The existance of these things does not alter the fact that variables must be explicity-declared and their types established at compile-time (modulo dynamic
, of course).
Explicit variable declaration removes a large class of bugs that sometimes occur in programs written in languages that do not require variable declarations.
In my humble opinion, I think it is safe to assume that they didn't (or couldn't) modify the C# grammar so significantly as to allow the omission of a (type or "var") token in the variable declaration when they added type inference to the language. (This would have consequences for function declarations also.)
I think such a change would have a ripple effect across the entire grammar and wreck backwards compatibility.
Maybe F# is a by-product of the grammar being changed at such a fundamental level ;)
精彩评论