Box packing and unpacking

Source: Internet
Author: User

Boxing: Converts a value type to a reference type.
Unboxing: Converts a reference type to a value type.

Look at whether the two types have occurred boxing or unpacking, to see whether there is an inheritance relationship between the two types.

 // int n = 10;  // object o = n;  //  boxing  // int nn = (int) o;  //             unpacking  int  n = 10              = n; //  boxing  
            // This place does not take place any type of packing or            unpacking // string str = "123";             // int n = convert.toint32 (str);

    Boxing and unpacking can affect system performance, and conversions between reference types and value types can be time-consuming if too many times. So in the program, we should try to avoid boxing and unpacking operation.

ArrayList list =NewArrayList (); //list<int> List = new list<int> (); //this cycle took place 1 million boxing operationsStopwatch SW =NewStopwatch (); //add data to ArrayList 00:00:02.4370587//adding data to the list 00:00:00.2857600SW.            Start ();  for(inti =0; I <10000000; i++) {list.            ADD (i); } SW.            Stop (); Console.WriteLine (SW.            Elapsed); Console.readkey ();

    From the above code we can see that when adding data to the ArrayList, the boxing operation takes place every time. This increases the time that the program runs.

  

Box packing 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.