-2

In C# Int type is derived from ValueType. ValueType is derived from Object type. Thus Int is an Object.

If every value type variable is already an Object, then what actually happens during boxing/unboxing?

TOP KEK
  • 2,593
  • 5
  • 36
  • 62
  • There is no type `Int`. Perhaps you mean `System.Int32` ? – Ben Voigt Dec 22 '14 at 22:30
  • 2
    I suppose your question will be very quickly downvoted to minus infinity, and then closed. – dymanoid Dec 22 '14 at 22:30
  • Read a bit first. This for instance http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx – John Dec 22 '14 at 22:31
  • 1
    There is a particular memory layout for `Object` instances. To save space and for compatibility with native code (COM, p/invoke, etc), instances of value types do not conform to that layout. – Ben Voigt Dec 22 '14 at 22:31
  • http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx does not provide answer for my question. – TOP KEK Dec 22 '14 at 22:32
  • 8
    @dymanoid this is not a bad question, especially for those who are somewhat new to C#. It does show research effort, as the author has referenced MSDN boxing/unboxing documentation. As a community we need to be more welcoming to new users. – Zaid Masud Dec 22 '14 at 22:40

2 Answers2

9

There is a particular memory layout for Object instances. To save space and for compatibility with native code (COM, p/invoke, etc), instances of value types do not conform to that layout.

"boxing" embeds the value inside an actual "object" instance with the expected layout. This enables polymorphic use by all the various functions that work on object instances and expect that interface.

It's really not correct to say that Int32 is a subclass of Object. "boxed Int32" is, but "unboxed Int32" instances do not have any base class subobject at all. (Among other things, object layout includes a pointer to the actual most-derived type of the instance. The type of value-type objects is determined by their relationship to something else, they do not contain the type metadata. Or a monitor. Or all the other goodies of object. Boxed versions do.)

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • But Int32 IS a subclass of Object. If I derive Cat from Animal and override it's Move method you would not say that Cat is not an instance of Animal just because some methods were overloaded. – TOP KEK Dec 22 '14 at 22:43
  • And why instances of value type do not conform memory layout? What is the difference? – TOP KEK Dec 22 '14 at 22:45
  • @Ask: I'm not talking about behavior that merely isn't identical, but that the behaviors don't exist. A value type can't have virtual methods because it had no vtable. It can't be used with the lock statement because it contains no monitor. – Ben Voigt Dec 22 '14 at 22:46
  • And if my Cat is derived from Animal, will it have vtable? – TOP KEK Dec 22 '14 at 22:49
  • @Kyle according to the. NET type hierarchy, it is derived from Object. The non obvious thing is that this relationship truly applies to the boxed Int32 instances only. – Ben Voigt Dec 22 '14 at 22:49
  • @Ask: well if animal is a C# class then yes Cat and animal have vtable. If it is a C# struct then you aren't allowed to inherit from it anyway. – Ben Voigt Dec 22 '14 at 22:51
  • But ValueType is a class, not a struct. Who is responsible for preventing me to inherit from it? Is it a compiler feature or CLR level restriction? – TOP KEK Dec 23 '14 at 06:19
  • Nothing prevents you from "inheriting" from `System.ValueType`. That's what the C# `struct` keyword does. In fact, a struct cannot inherit from anything else, not even another struct. You can't have a class inherit from a struct either, structs are sealed. Reuse only through composition. And "inheritance" from ValueType is just metadata, **unboxed instances of value types do not have a base class subobject**. – Ben Voigt Dec 23 '14 at 06:59
  • Value types are inherited, but don't have subobject? How is it possible? – TOP KEK Dec 23 '14 at 15:38
  • @Ask: Because what the C# specification calls "inheritance" is not what you think of as inheritance. This fake "inheritance" provides an implicit conversion (but it is a boxing conversion, not an upcast) and uses the "base class" field in the metadata. But this is just an optimization, using metadata memory that would otherwise be wasted, instead of introducing a new bitflag somewhere. – Ben Voigt Dec 23 '14 at 15:44
2

Think of Object simply as a base class from which ValueType inherits, not a type that represents the behavior of the value type. Boxing puts the value type into a reference type wrapper that then makes the value type behave like an object.

From MSDN:

Both reference and value types are derived from the ultimate base class Object. In cases where it is necessary for a value type to behave like an object, a wrapper that makes the value type look like a reference object is allocated on the heap, and the value type's value is copied into it.

Zaid Masud
  • 13,225
  • 9
  • 67
  • 88