Click here to Skip to main content
Click here to Skip to main content

Difference between Object, Dynamic and Var

, 30 Jul 2011
Rate this:
Please Sign up or sign in to vote.
In this post I am going to write the points about the three type of the variable Object, Var and Dynamic. Most of the developer not able to get what is difference between this three kind of variable.ObjectDynamicVarCan able to store any kind of value, because object is the base class of all type in

In this post I am going to write the points about the three type of the variable Object, Var and Dynamic. Most of the developer not able to get what is difference between this three kind of variable.

Object Dynamic Var
Can able to store any kind of value, because object is the base class of all type in .net framework. Can able to store any type of the variable, similar to old VB language variable. Can able to store any type of value but it require to initialize at the time of declaration.

Compiler has little information about the type

Compiler doesn't have any information about the this type of variable.

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.

Object type can be passed as function argument and function also can return object type Dynamic type can be passed as function argument and function also can return object 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.

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.
Casting is not require but you need to know the property and methods related to stored type No need to cast because compiler has all information to perform operation.
Cause the problem at run time if the stored value is not get converted to underlying data type.

Cause problem if the wrong method or property accessed because all the information about stored value is get resolve only at run time

Doesn't cause problem because compiler has all info about stored value.
Useful when doesn't have more information about the data type. Useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code. Useful when getting result out of the linq queries. In 3.5 framework it introduce to support linq feature.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

 
Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.
 
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
 

You can visit my blog

StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralMy vote of 3 PinmemberMystry Dengerous3-Mar-14 15:21 
GeneralRe: My vote of 3 PinprofessionalPranay Rana6-Mar-14 0:37 
GeneralRe: My vote of 3 Pinprofessionalkhurram ali lashari6-Mar-14 6:09 
GeneralMy vote of 5 PinprofessionalPratik Bhuva31-Dec-13 3:17 
QuestionNice one! check this blog also for more info about Var and Dynamic senthilvijayalakshmi.blogspot.in/2013/03/difference-between-var-and-dynamic.html [modified] Pinmembersenthilstayss7-Mar-13 17:55 
senthilvijayalakshmi.blogspot.in/2013/03/difference-between-var-and-dynamic.html

modified 8-Mar-13 0:06am.

QuestionNot an article Pinmemberaspnet_regiis16-Sep-12 23:20 
GeneralMy vote of 3 PinmemberGATzilla3-Aug-11 1:13 
GeneralRe: My vote of 3 PinmemberPranay Rana3-Aug-11 18:40 
GeneralRe: My vote of 3 PinmemberGATzilla21-Nov-11 16:25 
GeneralMy vote of 4 Pingroupsachin10d1-Aug-11 19:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 31 Jul 2011
Article Copyright 2011 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid