C#泛型List< >集合:创建、与数组转换,记录运行时间、装箱与拆箱、dictionary

来源:互联网 发布:闪光灯软件哪个好 编辑:程序博客网 时间:2024/06/08 13:52

List<int >  li=new List<int>();//创建泛型集合

List<int>与数组存放的类型都是固定的,但集合的长度是任意改变的,数组的大小是固定的。当变量的数量不确定时,采用集合

            //数组            int[] nums=new int[10];//没有赋值,里面存放的是0            string []  strs=new string[10];//未赋值,里面存放的是null            bool [] bols=new bool[10];// 未赋值,存放的是false            List<int> list=new List<int>();//未赋值,里面没有数据

   
         List<int>与int数组转换            li.Add(2);            // 泛型集合中一个特点是可以转成数组            li.Add(34);            int [] nint=li.ToArray();            foreach(int item in nint)            {                Console.WriteLine(item);            }            Console.WriteLine();

List<string>与string数组类型转换

            string[] str = { "I","am","so","happy"};            List<string> sl =str.ToList();             foreach(string item in sl)            {                Console.WriteLine(item);            }


记录运行时间

            Stopwatch sw = new Stopwatch();            sw.Start();###   记录运行时间的代码            sw.Stop();            Console.WriteLine(sw.Elapsed);

装箱和拆箱

值类型----->引用类型:装箱        拆箱:引用类型-------->值类型

两种类型存在继承关系,才有可能发生装箱和拆箱。我们在判断是否发生了拆装箱,首先判断这两种数据类型是否存在继承关系;装箱类型必须与拆箱类型一致。

  string  str=“123”’,int num=Convert.ToInt32(str);  没有发生装箱或拆箱

继承的话内存可能有交集,浪费时间

字典集合

        Dictionary<int, string> ndic=new Dictionary<int ,string>();        ndic.Add(1,"OK");        ndic.Add(2,"happy");Dictionary<int,string>的两种遍历方式        foreach (var item in ndic.Keys )        {            Console.WriteLine("{0}_____{1}",item,ndic[item]);        }        foreach (KeyValuePair<int ,string>  item in ndic)        {            Console.WriteLine("{0}---{1}", item.Key, item.Value);        }

装箱与拆箱

       {                     //拆装箱的最简单例子.拆装箱都耗费时间            int n1 = 100;            object ob = n1;// 装箱            int n2 = (int)ob;// 拆箱            //double n3 = (int)ob;//这里错误,必须用装箱时的类型拆箱            ArrayList arrayList = new ArrayList();//非泛型集合,没有固定的类型,会发生拆装箱,影响速度。            Stopwatch stArrayList = new Stopwatch();            stArrayList.Start();            for (int i = 0; i < 100000;i++ )            {                arrayList.Add(i);//这里add()里面的参数是object类型,将int:i---->object,相当于发生了装箱操作             }            stArrayList.Stop();            string timeArrayList = stArrayList.Elapsed.ToString();//记录下运行的时间,记录下拆装箱的时间            List<int> list=new List<int>();            Stopwatch stList = new Stopwatch();            stList.Start();            for (int i = 0; i < 1000000;i++ )            {                list.Add(i);//List<int> 类型已确定,未发生装箱            }            stList.Stop();            string timeList = stList.Elapsed.ToString();            //注意,下面没有发生拆装箱            father fa = new son();//这个叫隐式类型转换,不是装箱            son so = (son)fa;//这个叫显示类型转换,不是拆箱            //方法重载时,如果具有该类型的重载,那么就不叫拆箱或装箱            int n = 10;            Console.WriteLine(n);  //没有发生装箱,因为方法重载。解释一下,Console.WriteLine(object n)类型,但是因为方法重载了,存在Console.WriteLine(int n),所以调用此方法。            //接口与引用类型发生拆装箱,因为Int32继承了接口            IComparable com = n;//装箱            //拆装箱的次数            int num = 1;            string str = "hello";            double dou = 3.45;            object obj = num;            string st = str + num + dou;//相加相当于调用了string.Concat(object,object ,object ); 这里发生了2次装箱.string类型没有发生拆装箱            string stt=num+","+(int)obj;//这里发生了2次装箱 :num->object  (int)obj->object            string str1=num.ToString()+num.GetType().ToString();//1次装箱,GetType()不是虚方法,子类没有重写,所以调用的时候需要通过object.GetType()来调用.num-->object。没有Int32.GetType(),但是有Object.GetType();也有Int32.ToString()            Console.WriteLine("发生装箱运行时间: {0}",timeArrayList);            Console.WriteLine("未发生装箱运行时间: {0}",timeList);            Console.ReadKey();         }        public class father        {            string _name;            public string Name            {                get { return _name; }                set { _name = value; }            }        }        public class son : father        {            string _name;        }

原创粉丝点击