0

In java types like bool, int etc are primitives, i.e. they are inherently different from reference types, that's why java provides facilities for boxing and unboxing primitive types.

But in microsoft CLR, value types inherit from System.ValueType which itself inherits from System.Object. So then why is there a need for boxing and unboxing in microsoft CLR?

One reason might be that ValueType class provides some low level hacks which allow value types to be treated somewhat similar to C structs. But then, if value types are so different from reference types, why make ValueType inherit from Object?

Pardon me if I said something ignorant. I don't have much experience with language design. It's just that the type system of CLR doesn't look as coherent as that of java to me.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
saga
  • 1,933
  • 2
  • 17
  • 44
  • Possible duplicate of [How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes?](https://stackoverflow.com/questions/1682231/how-do-valuetypes-derive-from-object-referencetype-and-still-be-valuetypes) – hatchet - done with SOverflow Dec 04 '18 at 21:09
  • Key sentence from Eric Lippert's top answer to the question I linked to is " Inheritance has nothing to do with how things are copied." – hatchet - done with SOverflow Dec 04 '18 at 21:12
  • @hatchet The answer doesn't explain the fundamental difference between value types and reference types, value types are allocated on the stack, whereas reference types are created in head. How is that distinction made in CLR? – saga Dec 04 '18 at 21:19
  • Read the other answers too. Basically System.ValueType is special, and treated specially by the CLR. – hatchet - done with SOverflow Dec 04 '18 at 21:26
  • 1
    Boxing is required to implement the type system illusion that value types inherit from System.Object. Important in .NET 1.0 before generics became available and provide collection types like ArrayList etc. Pretty irrelevant today. – Hans Passant Dec 04 '18 at 23:24

0 Answers0