C # packing and unpacking,

Source: Internet
Author: User
Tags unpack

C # packing and unpacking,

Packing: a mechanism for converting a value type to a reference type.

Unboxing: obtains the address of the boxed field in the boxed object. It is worth noting that the unboxing is not a reverse packing process.

If you do not know how to check whether your code is packed, you can use the IL decompilation tool that comes with visual studio to check whether it is packed,

As shown in the following code, you can find box in the IL command. If it appears anywhere, it indicates that the box is packed.

 

Of course, the above Code is generally not written in this way. Here we only tell you how to use your own code to find the place where the packing occurs.

I commented out the above Code, rewritten a line of code, and re-opened the IL decompilation tool. What are the differences?

Why is there no box this time? Because no packing is performed in the code. Why? Because the Console. WriteLine () method has too many overloaded methods, and the method signature contains Int32, you must pay attention to the parameter type when writing code. Many seemingly similar codes are often hidden behind them.

So how do I know if there is any unpacking in the code?

You can also find the unbox in the il command.

Now everyone knows whether or not the code has been installed/unpacked. What have you done during the installation/unpacked process?

1 static void Main (string [] args) 2 {3 Int32 number = 5; 4 Object o = number; // boxed 5 number = 10; 6 Console. writeLine (number); 7 number = (Int32) o; // unpack 8 Console. writeLine (number); 9}

Packing:

1. Open up a space in the heap. The size of this space is the size of the memory occupied by the number value plus two additional members of the managed heap object (type object pointer and synchronous block index ).

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

3. Return the reference address of the object in the heap.

Then the object o obtains the address. Of course, the packing is completed at this time.

From this process, we can see that as long as the packing happens, we need to open up space in the heap memory, and then assign the field value to the past. This is why packing causes memory consumption, causes of performance impact, so we should avoid writing boxed code in the program as much as possible. Of course you have to write it when writing.

Unpack:

At the beginning of the article, we have already explained the process of unpacking. unpacking is the process of finding the address of the original value type in the heap. That is to say, it does not copy the value, so it is much lower than the consumption performance of packing.

Note the knowledge point when unpacking, that is, you need to split the value type when packing it. Otherwise, an InvalidCastException occurs.

Finally, can you see how many times the following code has been packed? If you don't pay attention to it, the code below is often written by many coders.

 1     class Program 2     { 3         static void Main(string[] args) 4         { 5             Point p = new Point(2, 5); 6             Console.WriteLine(p); 7             Console.WriteLine("{0};{1}", p, p); 8             Console.WriteLine(p.ToString()); 9             Console.WriteLine(p.GetType());10         }11     }12 13     struct Point14     {15         readonly Int32 x;16         readonly Int32 y;17         public Point(Int32 x, Int32 y)18         {19             this.x = x;20             this.y = y;21         }22         public override string ToString()23         {24             return string.Format("{0}-{1}", x, y);25         }26     }

Related Article

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.