C# 3.0 introduced Var type to declare implicitly typed local variable. An implicitly typed local variable is a strongly typed variable without explicit type declaration. The compiler looks at the type of expression on the right side of the assignment operator (=) and derives the type during compilation.

For example :  In below code, we have declared i using Var keyword without any type specified, which is known as implicitly typed variable. Here the compiler determines the type information during compilation.

In below, we have declared a with explicitly type information, this is known as explicitly typed variable.

Syntax & Usage

Following are some sample code demonstrating the use of this feature.

In the above code, the compiler looks at the type of expression on the right side of the assignment operator (=). Here 12345 is of type System.Int32 , hence compiler derives the variable i of type System.Int32

Var Type_Out_Put_1.1

In the first sample code above, compiler derives the type of var aarNum to be of System.Int32[] by looking at the right side expression. Similarly in the second example it derives the type of var arrString variable to be of System.String[].

In the first assignment, compiler derives var employee to be Generic.Dictionary`2[System.Int32,System.String]   and in the for each loop also, compiler automatically derives the type of element present in the dictionary.

In the above code sample, we see code simplicity of implicitly typed local variable feature, without this feature, we would have to specify Dictionary<int, string> on the both sides of the assignment operator.  Also if we change the generic type then no need to change on the both side. Only make the change the type on the right side and that will work.

Declaring a local variable using Var is just a syntactical shortcut. It allows the compiler to derive specific data type from an expression.

Use of Var in Anonymous Type :

Anonymous type in C# allows to declare type without any name. When you initialize a variable with an anonymous type, then its must to declare the variable as Var , this will allow you to access the properties of the object later.

OutPut:

Var Type_Out_Put_1.0

In the above example var employee is an anonymous type, here we have used the Var keyword which allows the compiler to derive the object property types at compile time.

Key Points :

Following are some of the key points, while using Var.

i – You must explicitly initialize a variable declared as Var type, here in below example var i will throw compile error.

ii – You cannot assign null to a Var type, below line will throw compile time error.

iii – You must declare Var as a local variable scope inside a method. Declaring Var as field level scope inside a class throws error. In below example var FirstName = "" throws an error as it is in field level scope. However  var fullName = "Amit kumar" doesn’t have any issue as it is in local variable scope.

iv – You cannot initialize multiple implicitly-typed variables in the same statement, below line will give compile time error.

v – You cannot assign an anonymous method to Var, below anonymous method assignment will throw compile time error.

However, below anonymous method assignment works fine.

Var and Dynamic type sounds similar but hey there are a lot difference between them . If you would like to explore on Dynamic type then you can do it here.

Thanks for your time 🙂 feel free to provide your feedback and do check my other blogs on Dynamic type in C# , Nullable type in C# , Anonymous type in C# and Reference Type and Value type .