C # Boxing and unpacking

Source: Internet
Author: User

Boxing: A mechanism for converting a value type to a reference type.

Unpacking: Gets the address of the boxed field in the boxed object; It is important to note that unpacking is not a boxing inverse process.

If you don't know how to see if your code is boxed, an easy way to do this is to see it through the IL Anti-compilation tool that comes with Visual Studio .

As shown in the code below, you can find boxin the Il command, and if it does, it will indicate that it has been boxed

Of course, the above code people generally do not write, here is just to tell you how to go to their own code to find the place where the boxing occurred.

I commented on the above code, rewrite a line of code, re-open the Il Anti-compilation tool, you see what is different?

Why is there no box this time ? Because there is no boxing in the code. Why is it? Because the Console.WriteLine () method has overloaded methods too many, where the method signature contains Int32, so write code must pay attention to the type of parameters. Many seemingly similar codes are often hidden behind the scenes.

So how do you know if there is a unboxing in the code?

The same can be found in the IL directive unbox

Now we all know that the code has not occurred in the loading/unpacking, the loading/unpacking process of what has been done?

1        Static voidMain (string[] args)2         {3Int32 number=5;4Object o = number;//Packing5Number =Ten;6 Console.WriteLine (number);7Number = (Int32) o;//Unpacking8 Console.WriteLine (number);9}

Packing:

1, open up a space in the heap, the size of this space is the value of number of the memory size plus the managed heap object two additional members (type object pointer and synchronization block index).

2. Copy the value of number to the space you just allocated.

3. Returns the referenced address of the object in the heap.

Then the object o gets the address, which, of course, has finished packing.

From this process, it can be seen that, as long as the occurrence of boxing, you need to go to the heap in memory space, and then assign the field to the past, which is why boxing can cause memory consumption, affecting performance reasons, so should be in the program as far as possible to avoid writing the occurrence of boxed code. Of course, you have to write when you write.

Unpacking:

At the beginning of the article has actually explained the unpacking process, unpacking is to find the original value type of address in the heap process. In other words, it does not replicate the value, so it is much smaller than the boxed consumption performance.

When unpacking, there is a knowledge point is also need to pay attention to, that is, you put what type of value to be torn down when the demolition of what type, otherwise it will occur InvalidCastException exception.

Finally, can you see that the following code has been boxed several times? If you don't pay attention, a lot of pups often write the following code.

1     class Program2     {3         Static voidMain (string[] args)4         {5Point P =NewPoint (2,5);6 Console.WriteLine (p);7Console.WriteLine ("{0}; {1}", p, p);8 Console.WriteLine (p.tostring ());9 Console.WriteLine (P.gettype ());Ten         } One     } A  -     struct Point -     { the         ReadOnlyInt32 x; -         ReadOnlyInt32 y; -          PublicPoint (Int32 x, Int32 y) -         { +              This. x =x; -              This. y =y; +         } A          Public Override stringToString () at         { -             return string. Format ("{0}-{1}", x, y); -         } -}

C # Boxing and unpacking

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.