Does unboxing just return a pointer to the value within the boxed object on the heap?
Posted
by Charles
on Stack Overflow
See other posts from Stack Overflow
or by Charles
Published on 2010-06-05T00:53:07Z
Indexed on
2010/06/05
6:12 UTC
Read the original article
Hit count: 260
I this MSDN Magazine article, the author states (emphasis mine):
Note that boxing always creates a new object and copies the unboxed value's bits to the object. On the other hand, unboxing simply returns a pointer to the data within a boxed object: no memory copy occurs. However, it is commonly the case that your code will cause the data pointed to by the unboxed reference to be copied anyway.
I'm confused by the sentence I've bolded and the sentence that follows it. From everything else I've read, including this MSDN page, I've never before heard that unboxing just returns a pointer to the value on the heap. I was under the impression that unboxing would result in you having a variable containing a copy of the value on the stack, just as you began with. After all, if my variable contains "a pointer to the value on the heap", then I haven't got a value type, I've got a pointer.
Can someone explain what this means? Was the author on crack? (There is at least one other glaring error in the article). And if this is true, what are the cases where "your code will cause the data pointed to by the unboxed reference to be copied anyway"?
I just noticed that the article is nearly 10 years old, so maybe this is something that changed very early on in the life of .Net.
© Stack Overflow or respective owner