Author : Rahul
Last Modified : 20-Sep-2020
Complexity : Beginner

Var and Dynamic keyword in C#


Var

It introduced in C# 3.0. It is declared without specifying the .NET type explicitly. The type of the variable is automatically resolved by the compiler at compile time from the value used to initialize the variable. It is designed to handle some special-case situation like LINQ. It is created using the var keyword.

Example:

// C# program for var keyword
using System; 

class Test { 

    static public void Main()
    { 

        var val1 = 100;
        var val2 = "Rahul";
        var val3 = 35.60d;
        var val4 = true;
        var val5 = 'a'; 

        Console.WriteLine("Type of val1 is : {0} ", val1.GetType()); 

        Console.WriteLine("Type of val2 is : {0} ", val2.GetType()); 

        Console.WriteLine("Type of val3 is : {0} ", val3.GetType()); 

        Console.WriteLine("Type of val4 is : {0} ", val4.GetType()); 

        Console.WriteLine("Type of val5 is : {0} ", val5.GetType());
    }
}

Output:

Type of val1 is : System.Int32
Type of val2 is : System.String 
Type of val3 is : System.Double 
Type of val4 is : System.Boolean 
Type of val5 is : System.Char 

 

Dynamic

It introduced in C# 4.0. It is used to avoid the compile-time type checking. Instead of resolving the type of the variable at compile-time, the compiler resolved the type of the variable at run time. It is created using dynamic keyword.

Example:

// C# program for dynamic keyword
using System; 
  
class Test { 
  
    static public void Main() 
    { 
  
        dynamic val1 = "Rahul"; 
        dynamic val2 = 1000; 
        dynamic val3 = 32.00; 
        dynamic val4 = false; 
  
        Console.WriteLine("Type of val1: {0}", val1.GetType().ToString()); 
  
        Console.WriteLine("Type of val2: {0}", val2.GetType().ToString()); 
  
        Console.WriteLine("Type of val3: {0}", val3.GetType().ToString()); 
  
        Console.WriteLine("Type of val4: {0}", val4.GetType().ToString()); 
    } 
} 

Output:

Get the actual type of val1: System.String
Get the actual type of val2: System.Int32
Get the actual type of val3: System.Double
Get the actual type of val4: System.Boolean

Below are some differences between var and dynamic keyword in C#:

Var Dynamic
It introduced in C# 3.0 It introduced in C# 4.0
It is statically typed. It is dynamically typed.
In case of var, the type of variable is identified by the compiler at compilation time. In case of dynamic the type of variable is identified by the compiler at run time.
It supports IntelliSense in visual studio. It does not support IntelliSense in visual studio.
In the case of var, the variable is initialized at the time of its declaration. In the case of dynamic, it is not mandatory to initialize the variable at the time of its declaration.
An exception is thrown if the variable is declared with var but not initialize at the time of declaration. Exception not thrown if the variable is declared with dynamic but not initialize at the time of declaration.