Object unpacking types types

I tried to understand this paragraph, but somehow I couldn’t virtualize it in my mind, someone, please clarify it a little:

Unboxing is not the exact opposite of boxing. The unpacking operation is much cheaper than boxing. Unboxing is really just the operation of getting a pointer to the type of raw value (data fields) contained within an object. In fact, a pointer refers to an unpacked piece in a boxed instance. Thus, unlike boxing, unboxing does not include copying any bytes in memory. Having made this explanation, it is important to note that the unpacking operation is usually followed by copying the fields.

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

+5
source share
1 answer

To tag an int, you need to create an object on the heap large enough to store all the data stored in the structure. Highlighting a new object on the heap means the GC works to find the spot, and works for the GC to clear / move it during and after its life. These operations, although not too expensive, are also not cheap.

To free up the value type, all you do is de-reference the pointer, so to speak. You just need to look at the link (this is what you have object) to find the location of the actual values. To look at a value in memory is very cheap, so this paragraph says that “unpacking” is cheap.

Update:

unboxed , . :

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

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

MyStruct obj, , , . LIkewise / , . , . , , .

+6

All Articles