VAR - DYNAMIC DIFFERENCE C#
1. The Var(Implicit typed local variable) keyword is used to define local variables.In case of Var , the underlying data type is determined at compile time itself based on the initial assignment.Once the initial assignment has been made with Var type , then it will become strongly typed.If you try to store any incompatible value with the Var type it will result in compile time error.
Example:
Var strNameList=new List<string>(); By using this statement we can store list of names in the string format.
strNameList.add("Senthil");
strNameList.add("Vignesh");
strNameList.add(45); // This statement will cause the compile time error.
But in Dynamic type, the underlying type is determined only at run time.Dynamic data type is not checked at compile time and also it is not strongly typed.We can assign any initial value for dynamic type and then it can be reassigned to any new value during its life time.
Example:
dynamic test="Senthil";
Console.Writeline(test.GetType()) // System.String
test=1222;
Console.Writeline(test.GetType()) // System.Int32
test=new List<string>();
Console.Writeline(test.GetType()) //System.Collections.Generic.List'1[System.String]
It doesn't provide IntelliSense support also.It doesn't give better support when we give work with linq also.Because it doesn't support lambda expressions ,extension methods and anonymous methods.
Use Dynamic when:
It is better to go with dynamic type , only when our application depends heavily on Reflection services.By using Dynamic we can reduce the amount of code while we are working with reflection.
If your application needs to communicate with COM components like Microsoft Office Products(i.e.MS Word,MS Excel), then we can prefer using Dynamic.
If your application needs to communicate with some dynamic languages like Iron Ruby or Iron Python, then we can prefer using Dynamic.
Use Var when:-
We work with linq.Because many linq queries will return only enumeration of anonymous classes..
2. We can use the Var keyword only for the local variables.We can't use it as a class level variables,method's parameter or method's return type. But we can use the Dynamic type as a Class level variables, local variables,dynamic property ,dynamic parameter and dynamic return type.
{
private var MyVarField;//Not possible
private dynamic MyDynField;// Possible
public var MyVarProperty{get;set;}//Not possible
public dynamic myDynProperty{get;set;}// Possible
public var MyVarMethod(var param1) //Not possible
{
var dynInt=25; // Possible
if(param1 is int)
{
return param1+dynInt;
}
}
public dynamic MyDynamicMethod(dynamic d1) // Possible
{
if(d1 is int)
{
return d1;
}
}
}
3.If you try to call non existing members(i.e variables or methods and so on) with dynamic type , it won't throw any compile time error. It will throw only run time error as it is not checked at compile time. But in case of var type, we can restrict in the compile time itself.
Example:
dynamic d=new MyObject();
d.CallNonExistingMethod()// Won't throw compile time error..will throw run time error.
// Unhandled Exception: Microsoft.Csharp.RuntimeBinder.RuntimeBinderException
// MyObject doesn't contain a definition for 'CallNonExistingMethod'
No comments:
Post a Comment