using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; namespaceAlgorithmTest07_拆装箱 { classProgram { staticvoidMain(string[] args) { int n = 100000000; Stopwatch t1 = new Stopwatch();//计时器 Stopwatch t2 = new Stopwatch();//计时器 Console.WriteLine("测试值类型对象int"); t1.Start();//开始计时 List<int> L = new List<int>(); for (int i = 0; i < n; i++) { L.Add(i);//不发生装箱 int x = L[i];//不发生拆箱 } t1.Stop();//停止计时 Console.WriteLine(t1.ElapsedMilliseconds + "ms"); t2.Start();//开始计时 ArrayList aL = new ArrayList(); for (int i = 0; i < n; i++) { aL.Add(i);//发生装箱[值类型(int)转引用类型(object),ArrayList是object引用类型] int x = (int)aL[i];//发生拆箱引用类型强转值类型 } t2.Stop(); Console.WriteLine(t2.ElapsedMilliseconds + "ms"); Console.ReadKey(); } } }