Question
Difference between "var" and "object" in C#
Is the var
type an equivalent to Variant
in VB? When object
can accept any datatype, what is the difference between those two?
45 33039
45
Question
Is the var
type an equivalent to Variant
in VB? When object
can accept any datatype, what is the difference between those two?
Solution
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. The following two declarations of i
are functionally equivalent:
var i = 10; //implicitly typed
int i = 10; //explicitly typed
var isn't object
You should definitely read this : C# 3.0 - Var Isn't Object
Solution
For more details have a look at http://www.codeproject.com/Tips/460614/Difference-between-var-and-dynamic-in-Csharp