Вы находитесь на странице: 1из 4

2/19/2014

Difference between Object, Dynamic and Var - CodeProject

Articles Languages C# General

Difference between Object, Dynamic and Var


By Pranay Rana , 31 Jul 2011
4.50 (10 votes)

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 type of value but it require to initialize at the time of declaration.

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.

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
http://www.codeproject.com/Articles/233553/Difference-between-Object-Dynamic-and-Var?display=Print 1/4

2/19/2014

Difference between Object, Dynamic and Var - CodeProject

assigning to object type and converting to original type called as Boxing and Un-Boxing for value type Casting is not require but you and for the reference type its casting of need to know the property and types. It's actually increasing the methods related to stored type 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. Cause the problem at run time if the stored value is not get converted to underlying data type.

No need to cast because compiler has all information to perform operation.

Cause problem if the wrong method or property accessed because all the information about stored value is get resolve Doesn't cause problem because only at run time 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

http://www.codeproject.com/Articles/233553/Difference-between-Object-Dynamic-and-Var?display=Print

2/4

2/19/2014

Difference between Object, Dynamic and Var - CodeProject

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:

26 Apr 2010: Best ASP.NET article of March 2010 23 Aug 2010: Best ASP.NET article of July 2010 2nd In ASP.NET article of December 2010 3rd In C# article of December 2010 4th In Overall article of December 2010 4th In Best overall article of February 2011 5th In Best C# article of February 2011 4th In Best ASP.NET article of April 2011

Follow on

Twitter

LinkedIn

Comments and Discussions


14 messages have been posted for this article Visit http://www.codeproject.com/Articles/233553/Difference-between-Object-Dynamic-and-Var to post and view comments on this article, or click here to get a print view with messages.
Permalink | Advertise | Privacy | Mobile Web04 | 2.8.140216.1 | Last Updated 31 Jul 2011 Article Copyright 2011 by Pranay Rana Everything else Copyright CodeProject, 1999-2014 Terms of Use
3/4

http://www.codeproject.com/Articles/233553/Difference-between-Object-Dynamic-and-Var?display=Print

2/19/2014

Difference between Object, Dynamic and Var - CodeProject

http://www.codeproject.com/Articles/233553/Difference-between-Object-Dynamic-and-Var?display=Print

4/4

Вам также может понравиться