Difference between var and dynamic in C#

var
  • Introduced in C# 3.0
  • At compile time, the compiler decide the type of variable which is declared. 
  • Need to initialize at the time of deceleration.
  • Visual Studio shows intellisense as the type of variable assigned is known to the compiler.
  • We cannot assign two data type in one variable since the compiler has already assign the data type to the value. 
     that is :
       
var obj1=1;
obj1="hello";

   will through an error.


dynamic


  • Introduced in C# 3.0
  • At runtime time, the compiler decide the type of variable which is declared. 
  • No need to initialize at the time of deceleration.
  • Visual Studio intellisense is not available since the type of variable assigned is unknown to the compiler, which will be known only at the run time.
  • We can assign two data type in one variable since the compiler recreate the type when another data type is assigned to the variable.



Comments