In my mind this is what I think of as boxing and unboxing. Nothing more. Can someone confirm that this is correct?
No.
While the general idea is right, it isn't quite correct. Boxed values are complete objects which conform to the memory layout for System.Object
. This means a v-table pointer (which provides the type-specific overloads for System.Object
virtual methods such as Equals
and GetHashCode
as well as serving as a type tag to prevent unboxing to an incompatible type), and an (optional) synchronization monitor.
The actual address stored in a handle to boxed value doesn't point to the content, but to the attached metadata.
Every value-type in .net actually defines two different kinds of things: a storage-location type, and a heap-object type. The heap type will, from an external point of view, behave much like a class
class Holder<T> where T:struct
{
public T This;
public override String ToString() { return This.ToString(); }
public override bool Equals(object other) { return This.Equals(other); }
etc.
}
which wraps exposes all of the value type's public methods. The heap type will have some additional magic, however, since it will implement any interfaces which the underlying value-type implements [as above, by wrapping calls to the value-type method]. Further, the Type
associated with a heap object will be the same as the Type
associated with a storage location.
Note that value-type storage locations hold instances of a value types and behave with value semantics, but a reference-type storage locations hold heap object references (or else null) and behave with mutable reference semantics (note that all value types, when boxed, behave as mutable reference types); the system does not terribly-conveniently provide for mutation, but boxed value-type instances can be mutated without the underlying value type having any say in the matter.
© 2022 - 2024 — McMap. All rights reserved.
not a real question
which proves that you were either: Trigger happy or just wanted the question closed in the first place. – FeastC#
boxing is a much more common issue than that specific question and will help future visitors which is what user414076's argument was about. – Feastthe benefit of "googlers" is precisely what this site is about. This is certainly not a site intended to answer the question purely for the benefit of the one that asked it.
..... I entirely disagree. I have provided many C#, SQL, JavaScript, jQuery etc custom solutions that answered the OP's question and would provide no benefits to future visitors. – FeastWhat is boxing
. – Feast