0

It is said that valuetype derives from system.object. Why do object derived classes take more space than valuetype structures? Thanks in advance

paseena
  • 4,207
  • 6
  • 32
  • 51
  • 1
    `It is said that valuetype derives from system.object.` - I wonder where is this said and if you found some source claiming this I would recommend you avoiding it as a learning material. – Darin Dimitrov Mar 21 '11 at 23:05
  • 3
    @Darin - `ValueType` does derive from System.Object : http://msdn.microsoft.com/en-us/library/system.valuetype(v=VS.100).aspx. However, it and it's derived types are handled in special ways by the CLR and compilers such that it certainly doesn't act like it... but as the reference material says object is the base for everything, I see the poster's confusion (boxing is a complex topic) – Philip Rieck Mar 21 '11 at 23:12
  • @Darin have you some source claiming the opposite? I thought `ValueType` technically derives from `Object` and is even a reference type itself. But of course the conversion of any concrete valuetype into `ValueType` or `Object` is a boxing conversion. – CodesInChaos Mar 21 '11 at 23:13
  • @Darin Dimitrov: You mean like [MSDN](http://msdn.microsoft.com/en-us/library/system.valuetype.aspx)? – Paya Mar 21 '11 at 23:14

3 Answers3

4

Every object has an object header. That's 8 bytes on a 32-bit machine, 4 for the sync block and 4 for the type handle. A value type value only derives from System.Object when it is boxed. An int is 4 bytes when it unboxed, 12 bytes when it is boxed, +8 bytes for the header.

Check this answer to get more insight in what a boxed value type looks like.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • A value type *always* derives from Object, and boxing a value type doesn't turn it into an object, it's only stored inside an object. A boxed int uses 16 bytes when boxed on a 32 bit system, and 20 bytes on a 64 bit system. – Guffa Mar 21 '11 at 23:14
  • @Guffa - you can use the linked answer to see that this isn't correct. Create two objects. – Hans Passant Mar 21 '11 at 23:20
  • That only shows how the data is *stored*. A boxed value type still is a value type, it doesn't get any characteristics of an object. A mutable struct like `Point` doesn't become a mutable object when boxed, it's still a structure stored inside an object. – Guffa Mar 21 '11 at 23:33
  • @Guffa - not sure what point you're trying to make. When you talk of about a *size* of 16 bytes for a boxed int then how it is *stored* matters a great deal. I assume you saw it take 12 bytes on the GC heap in the debugger. Yes, the layout of the value type bits is exactly the same when it is boxed, also visible in the debugger. No disagreement there. Plus the 8 byte object header. – Hans Passant Mar 21 '11 at 23:42
0

Because value types are handled differently by the compiler. Eventhough they inherit from Object, they are not stored as objects.

Objects are stored on the heap, with an extra overhead of two pointers (8 bytes on a 32 bit system, 16 bytes on a 64 bit system). Value types are stored inline, either as part of an object, or in the stack frame of a method call, and there is no extra overhead.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

There's a bit of a little white lie going on most of the time with regard to value types inheriting from ValueType and through that from Object. An unboxed int or bool doesn't have anything stored with it that relates to that inheritance. However, it gains it if it's boxed (which will happen implicitly with some operations). Most languages hide this so they simply appear to be the same as any other object that derives from Object whenever we use them as such, but also act as simple types when we use them as those.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251