What is difference between Var and Dynamic keywords?
Var |
Dynamic |
1.
The var keyword was introduced in .net 3.0 framework |
The Dynamic keyword was introduced in .net 4.0 framework |
2. var is used to declare a variable, its data type is determined at the time of
its declaration that means it is decided at compile time. |
Dynamic is also used to declare a variable, however its data type is not
determined at the time of its declaration that means it is decided at run time. |
3. You cannot
change its type at run time. |
You can change
its type at run time. |
4. For e.g. if you declare a variable and assign a value(i.e. var variable1 =
1;)in that case compiler will considered it as integer and at run time if u
change its value to string, an error will be thrown conversion from string to
integer is failed. |
For e.g. if you declare a variable and assign a value (i.e. Dynamic variable1 =
1;) in that case compiler will not determine its type and run time you can
change its value from string to integer. |
5. The advantage of var : if u assign any value from any objects or variables
to var, you will not have to decide its type explicitly, the compiler will set
its data type based on assigned value and hence it useful for linq |
The advantage dynamic : since value can be assigned at run time you can changes
its data type by assigning value of type objects or variables at run time. |
6. One more thing about var to remember is, var cannot be declared at class
level and also it cannot be used as
return type for any function. |
However dynamic can be declared at class level and also it can be used as return
type for any function. |
7. Any invalid
operation is caught at compile time.
E.g.
public class ClsStudent
{
Public void FunPubStd1()
{}
}
var ObjLocClsStudent = new ClsStudent();
ObjLocClsStudent. FunPubStd1()
ObjLocClsStudent. FunPubStd2()
In above mentioned example compiler will throw
an error since it will not get function FunPubStd2 while compilation. |
Any invalid operation is caught at run time
E.g.
public class ClsStudent
{
Public void FunPubStd1()
{}
}
var ObjLocClsStudent = new ClsStudent();
ObjLocClsStudent. FunPubStd1()
ObjLocClsStudent. FunPubStd2()
In above mentioned example compiler will not
throw an error since it will not check at run time. |
Let's
take one real life example to differentiate between var and dynamic type and
also use of one over another.
E.g. let's imagine we are going to market to buy some vegetables and for that we
are carrying a small polythene container however we get a call from home to
bring 10 litters oil as well. Now problem arises here, how to carry 10 litters
oil since we don't have big tin container to carry a huge quantity of oil. You
know why this problem raised because while leaving home we were asked to bring
vegetables only so we carried only small polythene container and latter we were
asked to bring oil only which is not possible to carry in small polythene
container. So now just think if we would have such kind of container which is
used to carry anything from small to large solid and liquid item well.
The same problem may arise in coding also as while compile time we might not
know which types of data needs to be stored or carried So to overcome this
problem we have dynamic type which can be used where compile time we don't know
the type of data we are going to get.