So you define var you not need to convert in real type it automatially convert and you can directly us using all related property and methods form actual type.
Where in object we have to write a code for convert in real type then only we can access its related types properties and methods.
As far as I have searched for the difference I found that
var is implicitly defined and
object is explicitly defined.. the following example actually tells you the exactly what I mean..
foreach (object item in people)
{
Person myperson = (Person)item;
Response.Write(myperson.DisplayFullName());
Response.Write("<br/>");
}
//Note i havent casted the var item
foreach (var item in people)
{
Response.Write(item.DisplayFullName());
Response.Write("<br/>");
}
Thanks
Arun
Marked as answer by demoninside9 on Feb 02, 2012 06:11 AM
I wanted to know the difference between var and object in c#. They are same ?
var might resolve to an object type in the right circumstances in which case they are the same. However, you need to understand that var is just a shorthand for whichever type is being declared. You need to provide an assignment so that the compiler can
work out what the type should be. There is no "conversion" going on at all.
Take your example:
foreach (var item in people)
The datatype of item is completely dependent on whatever the datatype of people is. If people is a collection of type object, item will be an object. If it is a collection of type Person, it is a Person type. Then there is no need for the cast in your next line:
demoninside9
Participant
1182 Points
1697 Posts
var vs object
Feb 02, 2012 05:25 AM|LINK
hi all,
I wanted to know the difference between var and object in c#. They are same ? bcos i use both in same code. they give same result. see below code
foreach (object item in people) { Person myperson = (Person)item; Response.Write(myperson.DisplayFullName()); Response.Write("<br/>"); } and foreach (var item in people) { Person myperson = (Person)item; Response.Write(myperson.DisplayFullName()); Response.Write("<br/>"); }MetalAsp.Net
All-Star
112032 Points
18231 Posts
Moderator
Re: var vs object
Feb 02, 2012 05:34 AM|LINK
Bing found me this interesting link: http://www.interact-sw.co.uk/iangblog/2005/09/23/varisntobject
amitpatel.it
Star
7908 Points
1856 Posts
Re: var vs object
Feb 02, 2012 05:52 AM|LINK
var is implecit type and object is explicit type.
So you define var you not need to convert in real type it automatially convert and you can directly us using all related property and methods form actual type.
Where in object we have to write a code for convert in real type then only we can access its related types properties and methods.
Below are the good ref links with more details.
http://www.codeproject.com/Articles/233553/Difference-between-Object-Dynamic-and-Var
MCPD Enterprise and Web Application
MCTS Web, Window and Enterprise Application
Arunmhn
Participant
1876 Points
347 Posts
Re: var vs object
Feb 02, 2012 06:09 AM|LINK
As far as I have searched for the difference I found that
var is implicitly defined and
object is explicitly defined.. the following example actually tells you the exactly what I mean..
foreach (object item in people) { Person myperson = (Person)item; Response.Write(myperson.DisplayFullName()); Response.Write("<br/>"); } //Note i havent casted the var item foreach (var item in people) { Response.Write(item.DisplayFullName()); Response.Write("<br/>"); }Thanks
Arun
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: var vs object
Feb 02, 2012 06:20 AM|LINK
var might resolve to an object type in the right circumstances in which case they are the same. However, you need to understand that var is just a shorthand for whichever type is being declared. You need to provide an assignment so that the compiler can work out what the type should be. There is no "conversion" going on at all.
Take your example:
The datatype of item is completely dependent on whatever the datatype of people is. If people is a collection of type object, item will be an object. If it is a collection of type Person, it is a Person type. Then there is no need for the cast in your next line:
Object is a kind of catchall data type.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
demoninside9
Participant
1182 Points
1697 Posts
Re: var vs object
Feb 02, 2012 06:25 AM|LINK
Thanks Mike...