C # packing and unpacking

Source: Internet
Author: User

In the actual encoding process, sometimes the packing and unpacking operations occur. The following are the classes:

It should be noted that the type conversion is different from this. The convert method does not perform boxing or unpacking, but type conversion, including Int. parse.

Packing means that a copy of the value type is copied to the heap memory.

Unpack and find the location where the value type is stored directly in the reference type (the person object is of the reference type, but its age attribute is of the value type and is also stored in the heap memory ), in fact, we often use a value type variable to receive it after unpacking.

Example 1:

1 int n = 10; 2 console. writeline (n); 3 object o = N; // four consoles are packed at a time. writeline (INT) O); 5 console. writeline (o); // The output string "10" is equivalent to console. writeline (O. tostring ());

In the above Code, The first sentence is not boxed, because the console. the writeline () method has an int type overload; 3rd sentences are packed once; 4th (INT) O is split once, and the value after unpacking is received by values, if no console exists. if the writeline () method has an int type overload, it will be immediately packed again, because this overload does not occur. In the last sentence, the output is the string "10", which is equivalent to the console. writeline (O. tostring ()). Therefore, the constructor should be used to determine whether a sentence is packed or unpacked.

Example 2:

1             int n = 10;2 3             IComparable com = n;4 5             int m = (int)com;6 7             Console.WriteLine(m);

In the above Code, the second sentence is packed, and the third sentence is split, because the int type implements the icomparable interface.

Example 3:

1 int d = 999; 2 object o = D; // boxed 3 4 // What type is used for packing, and the same type must be used for unpacking. Otherwise, an exception 5 double d1 = (double) O; // unpack
6 console. writeline (D1 );

In Example 3, an error is reported during compilation, which indicates that int type is used before 2nd sentences are packed, while double type is used for unpacking.Note the type before packing and the type after unpacking. Otherwise, an error is reported..

Example 4:

1 int n = 100; 2 M1 (n); 3 4 string x = "A"; 5 double d1 = 10; 6 string y = "B "; 7 int n1 = 9; 8 string Z = "C"; 9 string full = x + D1 + Y + N1 + z; 10 console. writeline (full); 11 12 // define the function as follows: 13 static void M1 (double D) 14 {15 console. writeline (d); 16} 17 18 static void M1 (Object O) 19 {20 console. writeline (o); 21}

How many times have the above Code been packed and unpacked? First, let's look at the M1 () method. After decompiling, We know M1 (double) N). It can be seen that the M1 () method does not find the object type overload, instead, we found the double type overload similar to this, so there was no packing. The third sentence is decompiled to know the console. writeline (string. concat (new object [] {X, D1, Y, N1, z}); It packs double type variables D1 and INT type variables N1 into object type. Therefore, the above Code is packed only twice.

Performance impact of packing and unpacking:

Packing and unpacking should be avoided in daily coding because they will cause performance loss. See the following code:

 

1 arraylist alist = new arraylist (); 2 stopwatch Sw = new stopwatch (); 3 SW. start (); 4 for (INT I = 0; I <100000; I ++) 5 {6 alist. add (I); // The add method is packed once every time it is added. 7} 8 SW. stop (); 9 console. writeline ("time used:" + SW. elapsed );

 

Running result:

Let's change to another one:

1 List <int> alist = new list <int> (); 2 stopwatch Sw = new stopwatch (); 3 SW. start (); 4 for (INT I = 0; I <100000; I ++) 5 {6 alist. add (I); 7} 8 SW. stop (); 9 console. writeline ("time used:" + SW. elapsed );

Running result:

It can be seen that the performance impact is still quite large.

 

Summary:

I) Judgment basis for packing and unpacking:
1. occurs between the value type and the reference type;
2. Have a parent-child relationship.

2) judging whether a sentence is packed or unpacked should be viewed by the constructor;

3) What type is pre-packing and what type is required after unpacking; otherwise, an error is reported;

4) Avoid the performance consumption caused by packing and unpacking during code writing.

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.