var - Implicit typed local variable in C#:

C# 3.0 introduced the implicit typed local variable "var". Var can only be defined in a method as a local variable. The compiler will infer its type based on the value to the right of the "=" operator.

Example: Explicitly typed variable

int i = 100;// explicitly typed 
var i = 100; // implicityly type

The following example shows how var can have a different type based on its value:

Example: Implicit typed variable - var

static void Main(string[] args)
    var i = 10;
    Console.WriteLine("Type of i is {0}",i.GetType().ToString());

    var str = "Hello World!!";
    Console.WriteLine("Type of str is {0}", str.GetType().ToString());

    var d = 100.50d;
    Console.WriteLine("Type of d is {0}", d.GetType().ToString());

    var b = true;
    Console.WriteLine("Type of b is {0}", b.GetType().ToString());

Type of i is System.Int32
Type of str is System.String
Type of d is System.Double
Type of b is System.Boolean

Var can be used in the following different contexts:

  • Local variable in a function
  • For loop
  • Foreach loop
  • Using statement
  • As an anonymous type
  • In a LINQ query expression

Further Reading:

Points to Remember :

  1. var can only be declared and initialized in a single statement. Following is not valid:
    var i;
    i = 10;
  2. var cannot be used as a field type at the class level.
  3. var cannot be used in an expression like var i += 10;
  4. Multiple vars cannot be declared and initialized in a single statement. For example, var i=10, j=20; is invalid.