Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 13th, 2008, 1:17 PM   #1
Robocop
Programmer
 
Join Date: Sep 2004
Posts: 42
Rep Power: 0 Robocop is on a distinguished road
var vs object

Can somebody tell me what's the difference between the 'var' and 'object' keywords in c#?
Robocop is offline   Reply With Quote
Old Apr 13th, 2008, 1:36 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3 Jimbo is on a distinguished road
Re: var vs object

'object' is a keyword because someone thought it would be cool to have a keyword for the System.Object type. It's the same with 'string' (not to be confused with Object and String respectively, since those are the class names, not keywords).

var is a keyword to denote that the type is whatever the assignment expression returns. It can be re-typed through various assignments too. Take the following:
c# Syntax (Toggle Plain Text)
  1. var foo;
  2. foo = new String();
  3. foo = new List<int>();
The compiler will determine the type of foo each time it is assigned to. Works pretty handy for anonymous types too
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 13th, 2008, 11:42 PM   #3
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 373
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Re: var vs object

really ?
From what I've read ,once you go
var foo = "";
... you can never go back .

In other words foo = new List<int>() - won't work because the language is still strongly typed, and will say can't assign List<> to string .. or something like that.

So the var keyword is a simpler way to declare stuff . so you don't have to go:
List<Dictionnary<int,List<string>>> myWeirdList = new List<Dictionnary<int,List<string>>>();

And useful for anonymous types.
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Apr 14th, 2008, 12:55 AM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3 Jimbo is on a distinguished road
Re: var vs object

Ah, you're right. I take that back, it's typed only on the first assignment.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 14th, 2008, 1:01 AM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: var vs object

Phew. I got confused there. I remembered reading about var when they first introduced it to the language, and it looked interesting. Then you told me that C# was actually dynamically typed and it blew my mind. :p
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
compile a var name from another var dpsleep JavaScript and Client-Side Browser Scripting 6 Oct 19th, 2007 10:52 AM
Object reference error Jabo Visual Basic .NET 3 Oct 5th, 2007 2:37 AM
Library problem creating Direct3d Object Kilo C++ 9 May 30th, 2006 8:48 AM
Pushin an object into a vector slyadams C++ 18 Mar 30th, 2006 11:17 AM
No Object Instance? Kilo C# 12 Dec 16th, 2005 3:02 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:48 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC