Var Vs Dynamic In C#

Introduction

 
Before we directly jump into Var & Dynamic, we first need to understand the compile-time & run time execution behavior.
 
In C# some variables are assigned at a compile time; others at runtime.

Compile-time error

  • The compiler will throw an error if you make a mistake while writing code.
  • In the following example, we are trying to store string inside an int. And we were stopped by the compilation police.

    Var Vs Dynamic In C#

Runtime error

  • The runtime check wouldn't bother you with an error unless your program is in running state & your execution point has reached the line where the runtime error is supposed to occur.
  • Divide by Zero, this exception is checked at runtime. So let us try to do that.
  • Yellow rectangles justify the flow of code.

    Var Vs Dynamic In C#
Now let's see how the var variable works with compile time.
 

Var variable

 
Var is an implicitly typed variable because it is assigned at compile-time, based on type defined on the right-hand side.
 
var must be initialized at the time of declaration.
 
See the following example,
 
var is a number, var is a string & var is also a user-defined class
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            var it_Is_A_Number = 1;  
  6.            var it_Is_A_String = "String";  
  7.            var it_Is_A_UserDefined_Class = new SmartPhone() { Name = "IPhone X", OS = "IOS 11"};  
  8.   
  9.            System.Console.WriteLine("it_Is_A_Number = " + it_Is_A_Number + " : " + it_Is_A_Number.GetType());  
  10.            System.Console.WriteLine("it_Is_A_String = " + it_Is_A_String + " : " + it_Is_A_String.GetType());  
  11.            System.Console.WriteLine("it_Is_A_UserDefined_Class = " + it_Is_A_UserDefined_Class.Name + " : " + it_Is_A_UserDefined_Class.GetType());  
  12.        }  
  13.    }   
Var Vs Dynamic In C#
 
Type of assigned var variable cannot be changed to another type after assigning it.
 
In the above example, if we try to change the type of it_Is_A_Number variable from int to string we will get a compile-time error,
 
Var Vs Dynamic In C#
 
Intellisense works with var variable because the compiler knows what type it is. So it can support Intellisense of that type.
 
Var Vs Dynamic In C#
 

Dynamic variable

 
It skips compile-time type checking. Instead, it checks the type at the run time & then assigns the right-hand side type to the left-hand side variable.
 
Dynamic variable does not need to be initialized at the time of declaration because they are going to be resolved at runtime.
  1. static void Main(string[] args)    
  2.   
  3.   {    
  4.       dynamic it_Is_A_Number = 1;    
  5.       dynamic it_Is_A_String = "String";    
  6.       dynamic it_Is_A_UserDefined_Class = new SmartPhone() { Name = "IPhone X", OS = "IOS 11" };    
  7.   
  8.       System.Console.WriteLine("it_Is_A_Number = " + it_Is_A_Number + " : " + it_Is_A_Number.GetType());    
  9.       System.Console.WriteLine("it_Is_A_String = " + it_Is_A_String + " : " + it_Is_A_String.GetType());    
  10.       System.Console.WriteLine("it_Is_A_UserDefined_Class = " + it_Is_A_UserDefined_Class.Name + " : " + it_Is_A_UserDefined_Class.GetType());    
  11.   
  12.   } 
Var Vs Dynamic In C#
 
The output is the same as var. Assign it at runtime or compile-time. There is no difference in their behavior.
 
Type of assigned dynamic variable can be changed to another type after assigning it. In the following example, first, we have assigned the dynamic variable with int, then changed it to a string.
  1. static void Main(string[] args)  
  2.         {  
  3.             dynamic it_Is_A_Number = 1;  
  4.             System.Console.WriteLine("it_Is_A_Number = " + it_Is_A_Number + " : " + it_Is_A_Number.GetType());  
  5.   
  6.             it_Is_A_Number = "It is string";  
  7.             System.Console.WriteLine("it_Is_A_Number = " + it_Is_A_Number + " : " + it_Is_A_Number.GetType());  
  8.         } 
It will run without any error. The dynamic variable holds a type of a variable which is assigned at the end.
 
Var Vs Dynamic In C#
 
Intellisense does not work with a dynamic variable because the compiler doesn't know what type it is. Type is going to be assigned at run time.
 
Var Vs Dynamic In C#
 

Conclusion

 
These are some basic difference between these two types.
 
In this article, we learned:
  • What var & dynamic variables are
  • What the difference is between these two


Similar Articles