What is the difference between var and dynamic keywords?
- var is early binded
- dynamic is late binded.
In order to understand the concept of early binding and late binding, let us check the below program-

In the above program, we are using var keyword to assign string type of value to a variable. Since the variable is of type string, hence while trying to print the length of the string on console, we have to use Length property.
When we try to do so, after typing a dot(.) after the variable "name", all the properties associated with the string variable are easily accessible along with the length property.
Now, in all the above process where is early binding explained?
Notice, the Length property of the string starts with capital "L" letter. If we try to use small "l", then the compiler will immediately show the error red line indicating that there is no property "length" that starts with small "l" for a string type.
This meas that at compile time itself we are able to know the error and able to resolve the same.
This is called early binding.
Consider the above figure which makes use of dynamic keyword. In this program, at the compile time, compiler does not know what is the type of the variable "name" and even intellisense does not work for dynamic keyword.
So if we type length property with small "l", then the solution will build successfully without any errors but when we run it, it shows an error that string does not contain a definition for length property that the user is trying to use.
Notice the error message carefully, we observe that at run time only, the type of variable is identified that it is a string type of variable.
This is called late binding.
Comments
Post a Comment