4

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?

Community
  • 1
  • 1
Andriy Drozdyuk
  • 58,435
  • 50
  • 171
  • 272

8 Answers8

16

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.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
2

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.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
1

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.

taylonr
  • 10,732
  • 5
  • 37
  • 66
1

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.

Simon Dufour
  • 184
  • 7
0

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.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0

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.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
0

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.

Jeffrey L Whitledge
  • 58,241
  • 9
  • 71
  • 99
0

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 ;)

Reuben Peter-Paul
  • 1,550
  • 3
  • 15
  • 25