7

Possible Duplicate:
c#: difference between “System.Object” and “object”

Although I am currently working with C#, this question could possibly apply to other languages.

Is there any difference between invoking Object vs. object? Specifically, I was creating an instance of Dictionary with the constructor:

Dictionary<String, Object> foo = new Dictionary...

The IDE automatically filled in new Dictionary<string, object>. I went back and changed my initial declaration but it got me wondering.

  • Are there any adverse reactions when I use uppercase String or Object versus lowercase string or object?
  • I'm assuming that uppercase refers to the class (so I can therefore access class methods) whereas lowercase simply refers to the type. Is this true?
Community
  • 1
  • 1
Jason L
  • 1,812
  • 2
  • 22
  • 43

2 Answers2

17

object is a keyword (alias) for System.Object, the same applies to string.

When compiled it will be exactly the same thing.

On the MSDN page for object it says the following:

The object type is an alias for System.Object in the .NET Framework. You can assign values of any type to variables of type object.

You can find a long list of all the keywords in C# on MSDN.

Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
  • 1
    I know with Java, I can use uppercase, like `Integer`, to call class methods like `Integer.parseInt()`. Am I reading your answer correctly if I assume that I can invoke a class method in a manner like `int.parseInt()` with C#? – Jason L Jul 05 '12 at 20:51
  • 1
    @Yawus, Yes, since it is just "aliases", you can use it exactly as you would with `System.Int32`. (note: in C# it's `int.Parse("10")`). – Filip Ekberg Jul 05 '12 at 20:54
  • string and object in C# are the same as the object when declaring, when you go to int and other "primitives" take care to understand to what "real" object they point to (Int32), so that you don't make mistakes such as thinking that int can be long, also the Upper case classes Int32, String etc. offer many static methods that make "sense" to use the Upper case notation... – Moshe Eshel Jul 05 '12 at 20:57
  • 2
    @Yawus In Java `int` and `Integer` are two different things. One is a primitive value type, another is a class that wraps it. In C# `int` and `System.Int32` are actually the same thing, exactly. One isn't a "wrapper" of the other. – Servy Jul 05 '12 at 21:05
0

There are some classes which have aliases like object, string, int, short and some more. They are all internally mapped to the real classes with the big capital letter.

Please note that some rare types depends on the platform. E.g. on x86 is IntPtr stores a 32 bit value while on x64 it stores a 64 bit value.

short is so far I know always mapped to Int16 and long to Int64.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rekire
  • 47,260
  • 30
  • 167
  • 264
  • `int` is always a Signed 32-bit integer. http://msdn.microsoft.com/en-us/library/exx3b86w.aspx – Filip Ekberg Jul 05 '12 at 20:52
  • `int` is *always* mapped to `Int32`, regardless of platform. `IntPtr`, on the other hand, is the "platform specific integer", and will be either 32 bit or 64 bits (or something else) depending on the platform. – dlev Jul 05 '12 at 20:53
  • You are right I mixed that up. – rekire Jul 05 '12 at 20:53
  • @rekire `IntPtr` is not an alias, and is not mapped to anything. It is a distinct type, whose size is dependent on the platform. – dlev Jul 05 '12 at 20:56
  • I was just now thinking about to rewrite the sentence. You are right again^^ – rekire Jul 05 '12 at 20:59