19

Possible Duplicates:
Is everything in .NET an object?
How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes?

Hi, I just do not get it. System.Object is (I think) reference type but all data types in .NET inherit from it. Also value types do as well. I do not understand it - value type has its value on the stack but its inherited from Object? Hope anyone could help me to understand

Community
  • 1
  • 1
Thomas
  • 2,575
  • 9
  • 31
  • 41

2 Answers2

12

You are correct that, while Object is a reference type, value types do inherit from it implicitly, as stated on msdn's reference for Value Types (according to the reference, they technically inherit from the ValueType class, which in turn inherits from Object.

C# made a special case for this so value types can benefit from Object's methods (like ToString) and properties. Also, this way, you can treat value types just like other reference types--nothing stopping you from plugging in an int inside an array of Objects.

Zach L
  • 16,072
  • 4
  • 38
  • 39
4

Ask yourself why you think it's strange that a type that is typically allocated on the stack would inherit from System.Object and I think you'll find you can't formulate a good reason.

If you think it's because an object's type defines where it is allocated, you're mistaken. What member of ValueType is responsible for defining its allocation mechanism? (What member of System.Object, for that matter?)

Type inheritance in .NET is supposed to comprise "is a" relationships: a string is an object, for example. Everything in the .NET world is an object, so an int is an object, a double is an object, etc.

You can think of this in terms of the Liskov substitution principle: if I write code that expects an object, I should be capable of dealing with any type that is an object—i.e., anything at all. My code should be equally comfortable with a string, an int, a List<int>, etc.

Also note that object guarantees certain members that all types have as a consequence of this: GetType, ToString, and GetHashCode.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • 3
    -1 it is remarkably bad answer. it doesn't explain anything. Also, "is-a" relationship does NOT mean that it has to derive from the same class. Likewise, an "object" doesn't mean it has to derive from `System.Object` class. It may not derive from anything, yet still can be an object. The question why `ValueType` derives from `Object` basically is a question of *semantic*, or type category : value-type vs reference-type. – Nawaz Jan 15 '13 at 07:20
  • @Nawaz Not true. Pointers don't derive from Object, thus they aren't objects. – IS4 Apr 19 '15 at 12:58
  • 1
    It is a famous C# myth that value types are typically allocated on stack. – RBT Dec 26 '16 at 03:25
  • @RBT: Are they not allocated on the stack? – Ini Dec 03 '17 at 19:48
  • 2
    No. *Most* of the times value types go to heap only. Only when they are local variables inside a method is when they will be allocated on stack. Please have look at [this](http://jonskeet.uk/csharp/memory.html) famous blog from Jon Skeet which will make thing crystal clear for you from all aspects. – RBT Dec 04 '17 at 01:11