Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unboxing Values Types from Objects

I've been trying to understand this paragraph, but somehow I couldn't virtualize it in my mind, some one please elaborate it little bit:

Unboxing is not the exact opposite of boxing. The unboxing operation is much less costly than boxing. Unboxing is really just the operation of obtaining a pointer to the raw value type (data fields) contained within an object. In effect, the pointer refers to the unboxed portion in the boxed instance. So, unlike boxing, unboxing doesn't involve the copying of any bytes in memory. Having made this important clarification, it is important to note that an unboxing operation is typically followed by copying the fields.

Richter, Jeffrey (2010-02-05). CLR via C# (Kindle Locations 4167-4171). OReilly Media - A. Kindle Edition.

like image 575
Tarik Avatar asked Apr 14 '12 02:04

Tarik


People also ask

When an object type is converted to a value type it is called unboxing?

Boxing and unboxing are important concepts in C#. The C# Type System contains three data types: Value Types (int, char, etc), Reference Types (object) and Pointer Types. Basically, Boxing converts a Value Type variable into a Reference Type variable, and Unboxing achieves the vice-versa.

What is boxing and unboxing give an example?

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.

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 required for unboxing in C#?

For an unboxing conversion to a given value-type to succeed at run-time, the value of the source argument must be a reference to an object that was previously created by boxing a value of that value-type. If the source argument is null or a reference to an incompatible object, an InvalidCastException is thrown.


1 Answers

In order to box an int you need to create an object on the heap large enough to hold all of the data that the struct holds. Allocating a new object on the heap means work for the GC to find a spot, and work for the GC to clean it up/move it around during and after its lifetime. These operations, while not super expensive, aren't cheap either.

To unbox a value type all you're doing is de-reference the pointer, so to speak. You simply need to look at the reference (which is what the object you have is) to find the location of the actual values. Looking up a value in memory is very cheap, which is why that paragraph is saying 'unboxing' is cheap.

Update:

While an unboxed value type will usually be copied to some other location right after being unboxed, that isn't always the case. Consider the following example:

public struct MyStruct
{
  private int value = 42;
  public void Foo()
  {
    Console.WriteLine(value);
  }
}

static void Main()
{
  object obj = new MyStruct();
  ((MyStruct)obj).Foo();
}

The MyStruct is boxed into obj but when it's unboxed it's never copied anywhere, a method is simply invoked on it. LIkewise you could pull a property/field out of the struct and copy just that part of it without needing to copy the whole thing. This might look a bit contrived, but it's still not entirely absurd. That said, as your quote implies, it's still likely to copy the struct after you unbox it.

like image 70
Servy Avatar answered Oct 21 '22 17:10

Servy