![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2004
Posts: 42
Rep Power: 0
![]() |
var vs object
Can somebody tell me what's the difference between the 'var' and 'object' keywords in c#?
|
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3
![]() |
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)
![]()
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#3 |
|
Professional Programmer
|
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 ! |
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3
![]() |
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> |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |