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.
Var strNameList=new List<string>(); By using this statement we can store list of names in the string format.
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.
Console.Writeline(test.GetType()) // System.String
Console.Writeline(test.GetType()) // System.Int32
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.
To know more about the Var and Dynamic, please have a look at my blog:-
Source: http://senthilvijayalakshmi.bl | Asked In: Many Interviews |