容易被忽视的装箱与拆箱,将会大大影响性能!
static void Main(string[] args)
{int totalCount;
ArrayList list = new ArrayList();
Console.Write("Enter a number between 2 and 1000:");
totalCount = int.Parse(Console.ReadLine());
//list.Add()初始调用发生装箱box。
//ArrayList方法签名为int Add(object value).所以传给该方法的任何值类型都会被装箱
list.Add((double)0);
list.Add((double)1);
for (int count = 2; count < totalCount;count++ )
{
//拆箱unbox ArrayList的索引运算符返回的object,
//因为是从ArrayList收集的(arraylist实例为object型)
//将两个值加到一起需要转型为double
list.Add((double)list[count - 1] + (double)list[count-2]);
//获取加法运算结果并放到ArrayList实例中再次装箱box