Saturday, February 27, 2016

C Sharp : Differences between Object, Var and Dynamic type

C# is very rich in data type. It provides data type like Object, var and dynamic which can be used to store any type of data.

In this post, I would like to explain the differences between var and dynamic data types along with some examples on their behavior.
VarDynamic
Introduced with C# 3.0Introduced with C# 4.0
It can store any type of value but It is mandatory to initialize var types at the time of declaration.It can store any type of the variable, similar to old VB language variable.
It is type safe i.e. Compiler has all information about the stored value, so that it doesn't cause any issue at run-time.It is not type safe i.e. Compiler doesn't have any information about the type of variable.
Var type cannot be passed as method argument and method cannot return object type. Var type work in the scope where it defined.Dynamic type can be passed as method argument and method also can return dynamic type.
Casting is not required as compiler has all information to perform operations.Casting is not required but you need to have the details of the properties and methods related to stored type.
Doesn't cause problem because compiler has all information about stored value.Cause problem if the wrong properties or methods are accessed because all the information about stored value gets resolved only at run time.
Useful when we don’t know actual type i.e. type is anonymous.Useful when we need to code using reflection or dynamic languages or with the COM objects, because you need to write less code.
  1. dynamic test = 1;  
  2. var test2=2;  
When we hover the mouse over “var” in the code above, Intellisense will show that compiler has correctly inferred the data type that it is an Int32. However, when we hover the mouse over “dynamic”, it would still continue to display as “dynamic” since dynamic types are not resolved until runtime.
  1. // Can I change the type of data stored in dynamic?   
  2.   
  3. dynamic test = 1;  
  4. test = "I am a string now"// compiles and runs just fine  
  5.   
  6. var test2 = 2;test2 = "I am a string now"// will give compile error  
This is one of the key differences between dynamic and var. A var is an implicitly typed variable that is inferred by the compiler, but it is just as strongly typed as if you had explicitly typed it yourself using “int test2 = 2;”. A dynamic variable on the other hand bypasses all compile-time type checking and resolves everything at runtime.

I will comment out the last line in the code above to get the code to compile, and add some code to verify the types of the variables.
  1. // Can I change the type of data stored in dynamic?   
  2. dynamic test = 1;  
  3. Console.WriteLine("Dynamic as " + test.GetType() + ": " + test);  
  4. test = "I am a string now"// compiles and run just fine  
  5.   
  6. Console.WriteLine("Dynamic as " + test.GetType() + ": " + test);  
  7. var test2 = 2;  
  8. //test2 = "I am a string now"; // will give compile error  
  9. Console.WriteLine("Var as " + test2.GetType() + ": " + test2);  
This produces the following output:
    Dynamic as System.Int32: 1
    Dynamic as System.String: i'm a string now
    Var as System.Int32: 2
 ------------------------------

C# is rich in data type. It provides three types Object, Var and Dynamic to store data of any type. In this article, I am trying to explore the differences among these three.
Difference between Object, Var and Dynamic type
Object
Var
Dynamic
Object was introduced with C# 1.0
Var was introduced with C# 3.0
Dynamic was introduced with C# 4.0
It can store any kind of value, because object is the base class of all type in .NET framework.
It can store any type of value but It is mandatory to initialize var types at the time of declaration.
It can store any type of the variable, similar to old VB language variable.
Compiler has little information about the type.
It is type safe i.e. Compiler has all information about the stored value, so that it doesn't cause any issue at run-time.
It is not type safe i.e. Compiler doesn't have any information about the type of variable.
Object type can be passed as method argument and method also can return object type.
Var type cannot be passed as method argument and method cannot return object type. Var type work in the scope where it defined.
Dynamic type can be passed as method argument and method also can return dynamic type.
Need to cast object variable to original type to use it and performing desired operations.
No need to cast because compiler has all information to perform operations.
Casting is not required but you need to know the properties and methods related to stored type.
Cause the problem at run time if the stored value is not getting converted to underlying data type.
Doesn't cause problem because compiler has all information about stored value.
Cause problem if the wrong properties or methods are accessed because all the information about stored value is get resolve only at run time.
Useful when we don’t have more information about the data type.
Useful when we don’t know actual type i.e. type is anonymous.
Useful when we need to code using reflection or dynamic languages or with the COM objects, because you need to write less code.

No comments:

Post a Comment