Why CLR do boxing when done casting from Structure to Object?

Every one know, when we takes Struct (Value Type) and pass to function, which wait for Object, boxing occurs.

But, struct inherits from ValueType, which inherits from Object...

Example:

ArrayList a = new ArrayList();
Point p = new Point(5,6);
a.Add(p);

In this example p boxed and added into ArrayList. But p already is Object (if you do "p is Object" you will get true). Does compiler check metadata to see all hierarchy of inheritances to see if some there is ValueType class to know if is value type and shall be allocated on stack? And, if it find in hierarchy of inheritance ValueType it do not continue see inner classes?

Example: compiler check Point metadata: from who inherits Point? TypeValue! ok, i do not continue - is value type

Jon Skeet
people
quotationmark

But p already is Object (if you do "p is Object" you will get true).

That doesn't mean the value of p is already a reference.

From the C# spec 7.10.10, the is operator - just the relevant part, where D is Point here, and T is object:

  • If T is a reference type, the result is true if D and T are the same type, if D is a reference type and an implicit reference conversion from D to T exists, or if D is a value type and a boxing conversion from D to T exists.

The emphasis is mine - we're in the last situation. There's a boxing conversion from Point to object, which is why is returns true... but that doesn't mean you can convert a value of type Point to a value of type object (i.e. a reference) without boxing...

people

See more on this question at Stackoverflow