Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The author of the original article must have been referring to what's happening down at the IL level. There exist two unboxing opcodes: <code>unbox</code> and <code>unbox.any</code>.</p> <p>According to MSDN, <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.unbox_any.aspx" rel="noreferrer">regarding <code>unbox.any</code></a>:</p> <blockquote> <p>When applied to the boxed form of a value type, the unbox.any instruction extracts the value contained within obj (of type O), and is therefore equivalent to unbox followed by ldobj.</p> </blockquote> <p>and <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.unbox.aspx" rel="noreferrer">regarding <code>unbox</code></a>:</p> <blockquote> <p>[...] unbox is not required to copy the value type from the object. Typically it simply computes the address of the value type that is already present inside of the boxed object.</p> </blockquote> <p>So, the author knew what he was talking about.</p> <p>This little fact about <code>unbox</code> makes it possible to do certain nifty optimizations when working directly with IL. For example, if you have a boxed int which you need to pass to a function accepting a ref int, you can just emit an <code>unbox</code> opcode, and the reference to the int will be ready in the stack for the function to operate upon. In this case the function will change the actual contents of the boxing object, something which is quite impossible at the C# level. It saves you from the need to allocate space for a temporary local variable, unbox the int in there, pass a ref to the int to the function, and then create a new boxing object to re-box the int, discarding the old box.</p> <p>Of course, when you are working at the C# level, you cannot do any such optimizations, so what will usually be happening is that the code generated by the compiler will almost always be copying the variable from the boxed object prior to making any further use of it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload