4

I am new to C#, please help me to understand the difference between the below statement:

var variable_name = new class_a(); // there is no error and is working fine

var variable_name; 
variable_name = new class_a(); // this line is throwing error

when I rewrote the statement as

class_a variable_name; 
variable_name = new class_a(); // this is working fine
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Abhishek
  • 763
  • 7
  • 18
  • [var keyword runtime or compile time?](http://stackoverflow.com/questions/3632918/var-keyword-runtime-or-compile-time) might help you understand this. – Liam Apr 15 '14 at 11:14
  • Just to add to the existing answers. "var" has an alternative the "dynamic" and it's type is resolved dynamically at runtime. If you try your second line with dynamic keyword it will work fine. However dynamic type somewhat reduces the visibility of your code and don't allow intellisense autocompletion. – Dmitry Apr 15 '14 at 11:29

5 Answers5

7

var is used to introduce an implicitly typed local variable. The type is known at compile time and is inferred from the type of the expression on the right hand side of the initialization statement. Using your example:

var variable_name = new class_a();

the compiler infers that new class_a() is an expression that yields an object of type class_a. Hence variable_name is declared as being of type class_a. This code is completely equivalent to

class_a variable_name = new class_a();

If the right hand side of the initialization is omitted, then there is no way for the compiler to infer the type. Hence the compilation error.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
5

var automatically infers the data type based on the value that the variable is initialized with.

var i = 3;    // 3 is an int; thus, i is declared as an int.

In your second example, there is no value specified, thus, inference is not possible.

var i;        // no data type can be inferred
user2864740
  • 60,010
  • 15
  • 145
  • 220
Heinzi
  • 167,459
  • 57
  • 363
  • 519
4

When you use var you don't specify the datatype, so you should assign some value for it so that the var 'variable' becomes of that datatype.

Only declaring and not assigning will not help and will throw an error in case of var.

Hope it clears your doubt.

Robert Langdon
  • 855
  • 1
  • 11
  • 27
2

Because var infers the type and is evaluated to a statically typed declaration at compile time, for example:

var var_name; // The compiler does not know what type var is. It has not been inferred. Error!

var var_name = new class_a(); // The compiler knows var has been inferred as class_a();

which equates to...

class_a var_name = new class_a();

Download ILSpy, compile your code, reflect it using ILSpy, and see what happens to your var declarations.

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
0

compiler wants to know the type of the variable when allocating a chunk of memory for it. the 3rd line do it and then gave it a value. the first line is ok because it's on one line and compiler knows the variable should be of type of class_a and it will allocate a memory and give the value of new class_a() object at the same time!
but the second one gives you error is because it will allocate a memory of no type! and you can't put any object of any type in that specific memory!

Shamim
  • 434
  • 4
  • 11