What is the differnce between var and dynamic keyword?
Suppose we have code like below.
var i= 123;
i="ABC";
print(i);
Above code will give compilation error "Cannot implicitly convert type 'string' to 'int'",
because when we initialize the var variable first time it will decide the datatype according
to value which we assign,
here first we assign i=123, so on the basis of value i is integer, intetger datatype is got
assigned and therefore later we cannot assign string value to it.
And suppose with dynamic keyword we have code like below
dynamic i= 123;
i="ABC";
print(i);
Above code gives the output as "ABC" because as name suggest the dynamic variable can deal
with any kind data type, no matter which first value is assign to this variable.
Suppose we have code like below.
var i= 123;
i="ABC";
print(i);
Above code will give compilation error "Cannot implicitly convert type 'string' to 'int'",
because when we initialize the var variable first time it will decide the datatype according
to value which we assign,
here first we assign i=123, so on the basis of value i is integer, intetger datatype is got
assigned and therefore later we cannot assign string value to it.
And suppose with dynamic keyword we have code like below
dynamic i= 123;
i="ABC";
print(i);
Above code gives the output as "ABC" because as name suggest the dynamic variable can deal
with any kind data type, no matter which first value is assign to this variable.