Why does C # need a "var" identifier?

Possible duplicate:
What is the meaning of the var keyword?

Why does C # need the identifier "var" for variables of type inferred type?

I mean, what is the problem with its simple:

a = 1; //vs var a = 1; 

Reading Programming in Scala:

A "variable type" syntax that you cannot simply leave aside the type would not have a marker to start defining more.

But what is the difference between leaving it in a: Int or int a ?

+4
source share
8 answers

At least one reason that comes to mind is that it forces the programmer to declare his intention to introduce a new variable, rather than assign an existing variable. This allows the compiler to detect numerous common coding errors.

+16
source

Consider this:

 class Foo { int a; void Bar() { var a = 1; } } 

Without the var keyword, the assignment will consist of a member of class a .

var introduces a uniquely new local variable.

+2
source

You must define the variable before use in C #. Of course, the compiler could detect when it reaches

 a = 1; 

That a is not yet defined and therefore defines the variable and assigns it a value of 1. However, this can lead to other problems that you have:

 MyOwnClass myVeryLongVariableNameThatIsUsedAllOverThePlace = new MyOwnClass(); myveryLongVariableNameThatIsUsedAllOverThePlace = input.GetNewClass(); 

Now you have 2 variables in which you thought you had.

+1
source

From MSDN:

It is important to understand that the var keyword does not mean "variant" and does not mean that the variable is freely typed or late binding. It just means that the compiler determines and assigns the most appropriate type.

The idea is to maintain C # reliability by preventing a random Implicitly typed local variable from happening .

+1
source

Technically, there is really no need. Other languages ​​allow you to combine declaration and appointment without special keywords.

If a = 1 was a valid syntax for both assignment and declaration, the code could become very confusing, which I think is why C # requires at least var .

0
source
Keyword

will tell the compiler not to try to resolve the identifier outside the scope of the method.

Can C # work without it? Yes, but it provides the discipline that is required for a strongly typed language.

0
source

Different programming languages ​​are designed for different purposes. C # is for writing large-scale, highly reliable software. Part of his approach to this is to use explicitly declared statically typed variables. The ability to invoke variable types was required by adding anonymous objects, which were required for LINQ query expressions. The existence of these things does not alter the fact that variables must be declared explicitly and their types are set at compile time (modulo dynamic , of course).

Explicit declaration of variables removes a large class of errors that are sometimes found in programs written in languages ​​that do not require variable declarations.

0
source

In my humble opinion, I think it is safe to assume that they did not (or could not) modify the C # grammar so much as to allow the (type or "var") token in the variable to be skipped when they added type inference to the language. (This may also have implications for function declarations.)

I think that such a change will have a ripple effect throughout the grammar and backwards compatibility.

Perhaps F # is a byproduct of grammar changing at such a fundamental level;)

0
source

All Articles