.NET Core CSharp articles 2-1 Intermediate boxing and unboxing

.NET Core CSharp Intermediate articles 2-1

This section contains the boxing and unboxing

Brief introduction

Boxing and unboxing is a relatively abstract concept. You can imagine a bunch of big trucks loaded with goods, he is composed of many workers will focus stacking goods loaded, for us not to open the cargo box, we can know that this is a cargo truck, which there are many goods, but what is the specific goods, we can know only open, and for the container, which can hold any volume smaller than their own goods, that is to say the cargo box has a wildcard character. Variable (Value type) in fact is the case, packing is to have actual data in C # packed into a reference type (Object), and the arrival of the cargo box we change, what we need to talk about this section and packing unpacking. Using boxing and unboxing function, is interchangeable with any value allowable value of type Object type, value type is concatenated with a reference type.

Packing

Boxing is to convert a value type to a reference type, in the previous basis for the type of talk, I have mentioned the value type is allocated on the stack, and reference types are allocated on the heap, and to note that this heap is managed heap. Corresponding to the managed heap garbage collection, the garbage collection heap memory that is a value type. Boxed value type is an interface to any type of object types or implicit conversion value of this type of implementation. Here the use of one of the most common scenario is to call a method parameter of type Object of containing the Object can support any type, because all types implicitly inherit from the Object class, so common. When you need a value type (e.g., Int32) incoming required packing. Another use is a non-generic container, likewise in order to ensure universal, and is defined as the element type Object. Thus, to the type of data value is added to the vessel, the packing needs.

This is a very simple packing operations:

double price = 13.53;
object temp = price;

This code may seem unusual harmony and simple, but have you ever thought what does this process take place?

Remember we talked about the process of creating the life cycle class or something? Packing virtually identical, boxed value type in an object instance allocated on the heap, and copied to the new value of the object. Carried out in three steps.

  • Newly allocated heap memory is managed, it is worth noting that this method need to add a memory table pointers and pointer SyncBlockIndex
  • The value type instance field copied to the newly allocated memory.
  • Hosted return address of the newly allocated heap objects. This address is a pointer to the object references.

It is apparent from the packing process, when packing, generate a new reference type, create type must be accompanied by a relatively large time loss. So we should try to avoid packing. Usually for packing the case, we can be avoided by generic or by overriding function. But suppose you want to transform the assembly code to the third party, you can not change, you can only be boxed. For the packing process, in C # it is implicit, if you want to observe this process, I suggest you use dnSpy or ILSpy decompile analysis of IL code.

However, a packing operation is only seemingly loss of performance, and occasionally also the role of one of the most common scenario is to call a method of the argument of type Object containing the Object can support any type as, for common. When you need a value type (e.g., Int32) incoming required packing. Another use is a non-generic container, likewise in order to ensure universal, and is defined as the element type Object. Thus, to the type of data value is added to the vessel, the packing needs.

And in particular, for the boxed object because the specified method can not call it directly, it must first unpack, then call the method, but unpacking again, will generate a new instance of the stack, and can not be modified boxed object. When these words I had to learn C # also entangled for some time, later understood why. Somewhat similar to the straightforward meaning you clone yourself, and you are exactly the same, but you two are the same person? Obviously not, you will not have human cloning operation you have any affect.

The following code you can try

struct Test
{
    public int x;
    public void test(int x)
    {
        this.x = x;
    }
}


Test t = new Test();
t.x = 100;
object a = t;//装箱
((Test)a).test(300);//x还是100不变,为什么

Unboxing

With respect to the packing, type (object) type is converted into a value type unpacking process is a reference, he said point is clear from the object type to a value type or types from the interface to realize the value of the type of interface explicit conversion. Unpacking checks the object instance, a given value to ensure that it is a boxed value type. To copy the value from a value type instance variables. But I went through a lot of information, for the unboxing, speaking very little, I guess, unpacking process, GetType this method will be called strict match.

double price = 13.53;
object obj = price;
double temp = (double) obj;

This is a process of unpacking, the converted value type is a reference type, then converted by the value of type reference type of process. First, get the managed heap belonging to address that part of the field value type, this step is unpacking the strict sense. The object reference value is copied to the value of the type instance located on the thread's stack. It is considered to be reciprocal and packing operations. Unpacking the strict sense, it does not affect performance, but the copy of the data associated with the operation after this operation will be the same as in boxing affect performance.

If my article help you, please recommend a focus point for me, a star point in the Github project page, thanks for the support

I will follow up on exercises and pictures

Github

BiliBili Home

WarrenRyan's Blog

Blog Park

Guess you like

Origin www.cnblogs.com/WarrenRyan/p/11267096.html