0

Possible Duplicate:
How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes?

I am able to write the code like this,

String s = 10.ToString();

ToString() will only come when that particular object is inheriting class 'Object'(ofcourse all the classes inherits Object in c#). Is it number 10 also inheriting the class 'object'. If so how?

Community
  • 1
  • 1
deen
  • 467
  • 2
  • 4
  • 10
  • This is a question that has been asked many times on this site. Try to look [here](http://stackoverflow.com/questions/4729094/value-types-inherit-from-system-object-why) Hope you find what your looking for. – phadaphunk Apr 05 '12 at 18:56
  • 1
    I disagree that this a duplicate of the previously linked question. – jason Apr 05 '12 at 19:00
  • 2
    @NullUserException: The previous question is about how can it be that value types can inherit from `object`. This question is around confusion around literals. See, the question is "Is it number 10 also inheriting the class 'object'. If so how?" Not "how can the value type `Int32` inherit from `object`" which clearly would be a duplicate of the previous question. – jason Apr 05 '12 at 19:07
  • My question is about Whether 10 is also being inherited. This couldnt be duplicate. So pls dont close it. Let the answer come – deen Apr 05 '12 at 19:11
  • *"Whether 10 is also being inherited."* That doesn't make sense. "10" is not a type. – NullUserException Apr 05 '12 at 19:13
  • 1
    @NullUserException: Right, which is why there is a question here that is different from those that people voted to justify closing this question. – jason Apr 05 '12 at 19:21
  • @Jason It's essentially the same question, and there are people who seem to agree with me. If you still think this should be reopened, feel free to bring the issue up on [meta](http://meta.stackoverflow.com). – NullUserException Apr 05 '12 at 19:44
  • @NullUserException: It's not even close to the same question. Knowing the answer to the previous question doesn't answer this one because it doesn't explain why you can call `ToString` on `10` because it doesn't explain what `10` is, and why it can be thought of as an instance of `object`. Please don't use appeal to "there are people that agree with me" as justification for your position. *Argumentum ad populum* is a logical fallacy. – jason Apr 05 '12 at 23:01
  • @Jason Let's just put it this way: in under 10 minutes this question got 3 "close as duplicate" votes, not counting mine. This is the way SO works. All you need to do is get 4 other people to agree with you and reopen the question. If it doesn't get deleted first, of course. – NullUserException Apr 05 '12 at 23:19
  • @NullUserException: Why are you talking to me like I don't understand this? Or that I even care about any of it. From my perspective, five people are wrong, including you. Why don't you instead try to convince me it actually is a duplicate? That's all I care about here. Not the number of people involved on one side or the other. – jason Apr 05 '12 at 23:22
  • @Jason let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9755/discussion-between-nulluserexception-and-jason) – NullUserException Apr 05 '12 at 23:22
  • Frankly, I am flabbergasted anyone thinks this is a duplicate. It's so obviously not. He's not asking about the type hierarchy or inheritance. He's asking about literals and how they are interpreted in a way that, say, let's you invoke `object.ToString` on them. Again, the previous question doesn't address this. Supposing one perfectly understood inheritance, which is the point of the previous question, but didn't understand the type system and how it treats literals could wonder this very point. – jason Apr 05 '12 at 23:28
  • Note that the OP even suggests he understands inheritance ("ofcourse all the classes inherits `Object` in c#) but is instead confused about what interpretation is being given to `10` such that it looks like something that inherits from `object` ("Is it number `10` also inheriting the class `object`?"). Sigh. – jason Apr 05 '12 at 23:38

2 Answers2

4

Is it number 10 also inheriting the class 'object'. If so how?

The literal 10 is, per the specification, interpreted by the compiler as an instance of Int32. Int32 inherits from object. Thus, 10 is an instance of a type that inherits from object. As such, it has an instance method named ToString.

jason
  • 236,483
  • 35
  • 423
  • 525
  • interpreted by the compiler -> I have nt compiled the code yet. – deen Apr 05 '12 at 19:14
  • 1
    @Ala: The "compiler" here refers to the fact that in Visual Studio there is are constantly running "processes" performing syntactical and semantical analysis of your code for a variety of purposes, one of which is the Intellisense feature. Here, the "compiler" performs the syntactic analysis of interpreting `10` as a literal instance of `Int32`, and using that analysis provides information to Intellisense that `10` is an instance of `Int32` and which methods are currently accessible including those that are accessible by way of the fact that `Int32` inherits from `object`. – jason Apr 05 '12 at 19:19
  • @Ala to summarize Jason's comment: the compiler runs continually as you type. But Jason's answer remains correct if you remove "interpreted by the compiler". Consider: "The literal `10` is, per the specification, an instance of Int32." – phoog Apr 05 '12 at 20:45
0

Quite simply, all primitives, including literals, are objects in .NET, and so inherit all methods that a .NET object has (including ToString()). See the MSDN documentation for a mapping of primitives to .NET objects: http://msdn.microsoft.com/en-us/library/ms228360(v=VS.80).aspx.

Greg Jackson
  • 118
  • 1
  • 10
  • 3
    @mydogisbox: This has nothing to do with boxing. Absolutely nothing. – jason Apr 05 '12 at 18:59
  • @mydogisbox: Directly from the MSDN documentation: "all primitive data types in C# are objects in the System namespace." Are you sure you're not thinking of Java, which uses wrappers? – Greg Jackson Apr 05 '12 at 19:01