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