tags:

views:

117

answers:

3

var keyword gets the type at the runtime or compile time?

or depends?

+9  A: 

Plain and simple: compile time

var isn't a type. The actual type is figured out at compile-time.

var variables are also known as Implicitly Typed Local Variables (C# Programming Guide)

Leniel Macaferi
i ve been trying to come up with a case that type wont be known until runtime, is that possible?
@user, no. If the compiler can't determine the type, compilation will fail.
Matthew Flaschen
You should check the new `dynamic` keyword in C# 4.0. http://msdn.microsoft.com/en-us/library/dd264736.aspx
Leniel Macaferi
+2  A: 

var type gets at compile time .

Var is an implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type

var i = 10; // implicitly typed
int i = 10; //explicitly typed

http://msdn.microsoft.com/en-us/library/bb383973.aspx

anishmarokey
If you're going to add quotes from the MSDN page, you might as well link to it.
rchern
A: 

The var keyword is implicitly typed. This means that it is strongly typed, but the compiler determines the type.

rchern