Take the 2-minute tour ×

How C# compiler interpret objects that specified by var keyword? When do we must use this keyword?

share|improve this question
Var is not a type. It is just saying that the type of the variable is the same as the type of the right hand side –  Rune FS Feb 18 '10 at 13:07

4 Answers 4

up vote 2 down vote accepted

In Var declaration,

Compiler infer the type from the assigned value


var a = 100;     // compiler assumes a is a integer
var b = "test";  // compiler assumes b is a string

Why do we need them (why cant we use objects directly)

Because object doesnt enforce type safety

  object objtest = 1;
  objtest = "test";

this works fine.

But var enforces type safety

  var a = 100;    
  a= "test";  

this doesnt compile, and will give the compile time error.

Where we can use them

  • Linq queries returning Anonymous types
  • And anywhere you want to define type safe variables (simplicity) . instead of writing something like this.


   RootClass rt = new RootClass ();
   List<RootClass > rt = new List<RootClass >();

you can write like this

var aaaaaa = new RootClass ();
var ls = new List<RootClass>();
share|improve this answer
Thanks Ramesh... –  masoud ramezani Feb 18 '10 at 13:08
@masoud ramezani, glad it helped.. :) –  RameshVel Feb 18 '10 at 13:14

There are times when you need to use var, for example, when using an anonymous type, such as in a LINQ query:

var results = context.People.Select(p => new {p.PersonID, p.Name});

See the new{} line in there? That's returning a class which is being generated at compile-time. Since this class has no name, the only way you can refer to it in your code is by using "var". This cuts down on you having to create tons and tons of special-purpose classes just for LINQ resultsets.

Under the hood, whether you use var just as a shorthand for another type, or if using an anonymous type, you are still doing static, compile-time type checking. Dynamic types are not being used. The compiler literally figures out what the type should be when you compile, so a program like:

var i = 12;
i = i + "foo!";

won't compile.

share|improve this answer
why don't we use object type for anonymous type? –  masoud ramezani Feb 18 '10 at 12:42
+1 for mentioning its proper use - too many people are getting lazy and using it all over the place. –  slugster Feb 18 '10 at 12:47
@slugster - to write off the use of var as laziness is really laziness on your part. Presumably you've never spent much time using languages such as Haskell or F# that make extensive use of type inference, and you've never tried writing C# using var wherever possible? Many who have find use of var everywhere much more readable, so it's a positive choice after not being lazy and evaluating the options. –  Greg Beech Feb 18 '10 at 12:51
+1 for Greg for getting it. -1 to slugster for not even trying –  Rune FS Feb 18 '10 at 13:05
@slugster - nothing to do with laziness. var myList = new List<Action<Foo>>(); is much cleaner and more readable than List<Action<Foo>> myList = new List<Action<Foo>>(); It removes redundancy, it's less code and it compiles to the same IL. –  Winston Smith Feb 18 '10 at 14:21

var (C# Reference)

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

share|improve this answer

If you mean the "var" keyword used in variable declaration then there is no such thing as "var type". This "var" is interpreted (at compile time) as type inferred from expression that is assigned to the variable.

share|improve this answer

Your Answer


