Var and Dynamic keyword in C#

Var was introduced in C# 3.0 and dynamic was introduced in C# 4.0. When we define a variable as var that means we cannot change its data type after first initialization and data type checking at compile time. When we define a variable as dynamic that means we can change its data type for any number of times during program execution and data type checking at run time.

  1. class vardynamic  
  2.     {  
  3.         public void show()  
  4.         {  
  5.             var a = "Hello var";  
  6.             dynamic b = “Hello dynamic”;  
  7.             Console.WriteLine("a: {0}", a);  
  8.             Console.WriteLine("b: {0}", b);  
  9.             //a = 10;  
  10.             //Console.WriteLine(a);  
  11.             b = 10;  
  12.             Console.WriteLine(b);  
  13.         }  
  14.     }   
In above class, I have defined two variable a and b with var and dynamic respectively. Both the variables first initialized as string data type. If I uncomment two lines, program will give compile time error because variable a is first initialized as string that later changing to integer data type which is not possible. But b can be changed to string data type to integer data type because it is declared as dynamic.