+ 1

Can anyone tell me difference between var and dynamic?

8th Sep 2016, 10:53 AM
jay kashyap
jay kashyap - avatar
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.
10th Sep 2016, 5:34 AM
Ivan G
Ivan G - avatar
+ 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
8th Sep 2016, 8:22 PM
Kailash Kothiyal
Kailash Kothiyal - avatar
0
ohh..I see
14th Sep 2016, 9:27 PM
Bubul Doley
Bubul Doley - avatar