Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unboxing does not create a copy of the value. Is this right?

Tags:

c#

.net

I was reading Microsoft's Class Room Training Materil. I read the following

Unboxing Unboxing is the opposite of boxing. It is the explicit conversion of a reference type to a value type. Unboxing retrieves a reference to the value type contained within an object. An unboxing operation involves checking the object instance to ensure that the object instance is a boxed value of the given value type. Then, the value from the instance is copied into the value type variable.

**

Unboxing returns a pointer to the data within a boxed object and does not create a copy of the data.

**

I dont really understand the line that i highlighted. it says that when unboxing boxed object, it does not create a copy, it just returns a pointer. If that is true, then a value type variable will be allocated in Heap right?

Ram

like image 925
RAM Avatar asked Sep 18 '10 22:09

RAM


People also ask

What is required for unboxing?

To start out in unboxing, you'll need a simple one-camera setup. You can even use a quality cell phone camera at first. Carefully consider the location where you'll shoot your videos. A clean background and good lighting, along with a table or countertop to display the products, are important for unboxing success.

What is the use of boxing and unboxing?

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the common language runtime (CLR) boxes a value type, it wraps the value inside a System. Object instance and stores it on the managed heap. Unboxing extracts the value type from the object.


2 Answers

In addition to what Guffa said, here is some additional information:

  • The “unboxing” operation described by the text you quoted describes the unbox CIL instruction. The CIL standard has this to say about unbox:

    Unlike box, which is required to make a copy of a value type for use in the object, 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

  • The unboxing conversions you use in C# are not compiled to unbox. They are compiled to another instruction called unbox.any:

    [...] the unbox.any instruction extracts the value contained within obj (of type O). (It is equivalent to unbox followed by ldobj.)

    In English, this means unbox.any does an unboxing operation (unbox) — which pushes a pointer onto the evaluation stack — followed by the copy operation (ldobj), which converts the pointer to the actual value contained in the value type and pushes that on the evaluation stack instead.

    For completeness, here is the description of ldobj:

    The ldobj instruction copies a value to the evaluation stack. [...] src is an unmanaged pointer (native int), or a managed pointer (&). [...]

    [Rationale: The ldobj instruction can be used to pass a value type as an argument. end rationale]

As far as I am aware, the C# compiler never uses unbox or ldobj, it always uses unbox.any to do unboxing, and ldind.* to dereference references (such as ref/out parameters).

like image 65
Timwi Avatar answered Oct 21 '22 01:10

Timwi


Well, it's true, but not the whole picture.

The unboxing itself only returns a pointer to the data, but you can't use that to access the data in C#. When you have unboxed a value in C#, you always copy it somewhere.

Example:

object o = 42; //box
int i = (int)o; //unbox

The unboxing iself gets the pointer to the value 42 in the object o, then the value is copied into the variable i.

like image 37
Guffa Avatar answered Oct 21 '22 01:10

Guffa