9.10.2012

Object vs Var vs Dynamic Keywords

Difference between Object, Var and Dynamic Keywords


Object
Var
Dynamic
Introduction:
Object keyword is introduced in C# 1.0
Introduction:
Var keyword is introduced in C# 3.0

Introduction:
Dynamic keyword is introduced in C# 4.0
Data Storage:
Can able to store any kind of value, because object is the base class of all type in .net framework.
Data Storage:
Can able to store any type of value but it require to initialize at the time of declaration.
Data Storage:
Can able to store any type of the variable, similar to old VB language variable.
Type Safety:
Compiler has little information about the type .
Type Safety:
It's compiler safe i.e compiler has all information about the stored value, so that it doesn't cause any issue at run-time.
Type Safety:
Compiler doesn't have any information about the this type of variable.

Supports as method argument type :
Object type can be passed as function argument and function also can return object type
Supports as method argument type :
Var type can not be passed as function argument and function can not return object type. This type of variable can work in the scope where it defined.
Supports as method argument type :
Dynamic type can be passed as function argument and function also can return object type
Need type casting on assignment :
Require to cast object variable to original type before using it. So this assigning to object type and converting to original type called as Boxing and Un-Boxing for value type and for the reference type its casting of types. It's actually increasing the overhead when we do this both operation.
Allows to perform operation of given type once it get cast any user defined or primitive data type.
Need type casting on assignment :
No need to cast because compiler has all information to perform operation.
Need type casting on assignment :
Casting is not require but you need to know the property and methods related to stored type
Stored Value:
Cause the problem at run time if the stored value is not get converted to underlying data type.
http://2.bp.blogspot.com/-AcTarSXqc08/TjKtgMQ-jiI/AAAAAAAACcY/ldRUTGHpTO4/s1600/Object+error.JPG
Stored Value:
Doesn't cause problem because compiler has all info about stored value.
Stored Value:
Cause problem if the wrong method or property accessed because all the information about stored value is get resolved only at run time
As source in for each iteration:
Not supported
As source in for each iteration:
Supported
As source in for each iteration:
Supported
Application Area:
Useful when doesn't have more information about the data type i.e., to hold objects as base class.
Application Area:
Useful when getting result out of the linq queries. In 3.5 framework it introduce to support linq feature.
Application Area:
Useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code.
Compilation Example:
Object o = new Button();
o.Width doesn’t compile
Compilation Example:
var o = new Button();
o.Width will compile
Compilation Example:
dynamic o = new Button();
o.Width and even o.xyz() will compile where xyz is not a member

References:



No comments:

Post a Comment