Introduction
In this blog, we are discussing the difference between the Dynamic and Var keywords.
If I try to put the deference in one sentence, Var is bound early. In other words, it's statically checked. Dynamic is late binded, in other words, dynamically evaluated.
Let's discuss this deference through a simple program.
In the below code, I have declared x as a var and assigned the string value. In the next statement, I am trying to grab a length of the string.
- static void Main(string[] args)
- {
- var x = "string value";
- int length = x.Length;
- }
Please check out the below snapshot.

Let's look at the dynamic example.
We can see in the below code snippet that we will not get any intelligence because dynamic is late binding.
- dynamic y = "string value";
- int length2 = y.Length;
One more difference is when we are dealing with the dynamic keyword, we will not get any compile-time errors. For example, in the below code, if we compile, we will not get any errors but in run time we will encounter the exception.
- dynamic y = "string value";
- int length2 = y.length;

Summary
In this blog, we have discussed the difference between dynamic and var with some simple programs.

Join the conversation! Your thoughts help the community grow.