Difference between Var and Dynamic Keywords

Many developers are very confused about how to differentiate between var and dynamic keyword.
Let's try to understand.

Open a New Project Like:

new
console

var _string = "Amit Kumar";

code

Here I defined a var type variable which contains string value.

And I want to get the length of this string here.

length
code

After that one more point here please pay attention (above screen shot) when we type the length property then it shows (local variable) string _string . It means _string is string data type. Due to this reason it is called early bounded .

Dynamic keyword:

code

In the above snippet I defined dynamic type d and store “Abhishek” after that get the length of dynamic type variable d when we type d.length then you can see he does not know what is data type of d .

code

Still it is not pointing out that it is string data type or other data type.

After that gets the name and length of dynamic data type shown like.

code
code

When we type d.Length, d is still here and we do not know whether d has length property or not;  it dynamically (runtime)goes and uses reflexation and uses Length property.

Due to this reason dynamic is known as late bounded,

code

When we typed d.length it brought exception because length is written with a small letter and it is not a property of string.

code

Conclusion : Var is early bounded, dynamic is late bounded.

Ebook Download
View all
Learn
View all