+ 1
Can anyone tell me difference between var and dynamic?
3 Answers
+ 3
Var tells compiler or visual studio to guess type of variable by itself using right side of your variable definition code. You cannot change variable type after definition.
For example:
var a = 1; // a will have int type
var b = "a string"; // b will have string type
var c = 1f; // c will have float type
So after all this you cannot write something like:
c = "I don't like floats";
Compile error will be raised in this case because c variable has float type, not string.
Dynamic type doesn't care about what type of data is stored at compile time. It will not provide you any intellisense support either. It will raise exceptions on runtime if something goes wrong.
Dynamic is like when you create an instance of some type and cast it to object type. The difference is that when you need to access instance property, dynamic allows you to access it without reverse casting from object to proper type.
+ 1
var need to be assigned some value on compile time
but dynamic can`t be on compile time
its take value on runtime on dynamically
0
ohh..I see