Boxing and Unboxing

来源:互联网 发布:linux 卡片电脑 编辑:程序博客网 时间:2024/05/01 20:33
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace Boxing
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
int count;
            DateTime startTime 
= DateTime.Now;
            ArrayList AyList 
= new ArrayList();

            
// 重复5次测试 
            for (int i = 5; i > 0; i--)
            {
                AyList.Clear();

                
// 将值类型加入myArrayList数组 
                for (count = 0; count < 5000; count++)
                {
                    Console.WriteLine(
"开始Boxing.....");
                    
//Boxing
                    AyList.Add(count); 
                }
                
                
// 重新得到值
                int j;
                
for (count = 0; count < 5000; count++)
                {
                    Console.WriteLine(
"开始Unboxing....");
                    
//Unboxing
                    j = (int)AyList[count];
                }
            }

            
// 打印结果
            DateTime endTime = DateTime.Now;
            Console.WriteLine(
"开始时间: {0} 结束时间: {1} 耗时: {2}", startTime, endTime, endTime - startTime);
            
            Console.WriteLine(
"按回车结束程序...");
        }
    }
}