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.
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.
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
- class Program
- {
- static void Main(string[] args)
- {
- var it_Is_A_Number = 1;
- var it_Is_A_String = "String";
- var it_Is_A_UserDefined_Class = new SmartPhone() { Name = "IPhone X", OS = "IOS 11"};
-
- System.Console.WriteLine("it_Is_A_Number = " + it_Is_A_Number + " : " + it_Is_A_Number.GetType());
- System.Console.WriteLine("it_Is_A_String = " + it_Is_A_String + " : " + it_Is_A_String.GetType());
- System.Console.WriteLine("it_Is_A_UserDefined_Class = " + it_Is_A_UserDefined_Class.Name + " : " + it_Is_A_UserDefined_Class.GetType());
- }
- }
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,
Intellisense works with var variable because the compiler knows what type it is. So it can support Intellisense of that type.
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.
- static void Main(string[] args)
-
- {
- dynamic it_Is_A_Number = 1;
- dynamic it_Is_A_String = "String";
- dynamic it_Is_A_UserDefined_Class = new SmartPhone() { Name = "IPhone X", OS = "IOS 11" };
-
- System.Console.WriteLine("it_Is_A_Number = " + it_Is_A_Number + " : " + it_Is_A_Number.GetType());
- System.Console.WriteLine("it_Is_A_String = " + it_Is_A_String + " : " + it_Is_A_String.GetType());
- System.Console.WriteLine("it_Is_A_UserDefined_Class = " + it_Is_A_UserDefined_Class.Name + " : " + it_Is_A_UserDefined_Class.GetType());
-
- }
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.
- static void Main(string[] args)
- {
- dynamic it_Is_A_Number = 1;
- System.Console.WriteLine("it_Is_A_Number = " + it_Is_A_Number + " : " + it_Is_A_Number.GetType());
-
- it_Is_A_Number = "It is string";
- System.Console.WriteLine("it_Is_A_Number = " + it_Is_A_Number + " : " + it_Is_A_Number.GetType());
- }
It will run without any error. The dynamic variable holds a type of a variable which is assigned at the end.
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.
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