Difference between var and dynamic in C#
var
dynamic
- Introduced in C# 3.0
- At compile time, the compiler decide the type of variable which is declared.
- Need to initialize at the time of deceleration.
- Visual Studio shows intellisense as the type of variable assigned is known to the compiler.
- We cannot assign two data type in one variable since the compiler has already assign the data type to the value.
var obj1=1;
obj1="hello";
will through an error.dynamic
- Introduced in C# 3.0
- At runtime time, the compiler decide the type of variable which is declared.
- No need to initialize at the time of deceleration.
- Visual Studio intellisense is not available since the type of variable assigned is unknown to the compiler, which will be known only at the run time.
- We can assign two data type in one variable since the compiler recreate the type when another data type is assigned to the variable.
Comments
Post a Comment