码迷,mamicode.com
首页 > Web开发 > 详细

.Net基础加强06

时间:2014-08-18 20:06:13      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   ar   art   

弱引用

集合 Hashtable Dictionary<K,V>

ArrayList  List<T>

   class Program
    {
        static void Main(string[] args)
        {
            //ArrayList list = new ArrayList() 
            //{ 
            //new Person(){Name="科比",Age=35},
            //new Person(){Name="詹姆斯",Age=30},
            //new Person(){Name="叶长种",Age=25},
            //};
            //list.Sort();
            //for (int i = 0; i < list.Count; i++)
            //{
            //    Person p= list[i] as Person;
            //    Console.WriteLine(p.Name); 
            //}
            //Console.ReadKey();

            int[] nums = {3,5,2,6,9,1 };
            for (int i = 0; i < nums.Length; i++)
            {
                for (int j = nums.Length-1; j > i; j--)
                {

                    //if (nums[j].CompareTo(nums[j-1])<0)
                    //{
                    //    int n = nums[j];
                    //    nums[j]=nums[j-1];
                    //    nums[j - 1] = n;
                    //}

                    IComparable com =(IComparable)nums[j];
                    if (com.CompareTo(nums[j - 1]) < 0)
                    {
                        int n = nums[j];
                        nums[j] = nums[j - 1];
                        nums[j - 1] = n;
                    }
                }  
            }

            for (int i = 0; i < nums.Length; i++)
            {
                Console.WriteLine(nums[i]);
            }

            Console.ReadKey();            
        }
    }

    public class Person:IComparable
    {
        public string Name
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
        public int CompareTo(object obj)
        {
            Person p = obj as Person;
            return this.Age - p.Age;
        }
    } 

装箱和拆箱

值类型直接转为为引用类型为装箱;引用类型直接转换为值类型为拆箱。

 class Program
    {
        static void Main(string[] args)
        {
            //int n = 10;
            //string s = n.ToString();//不是装箱
            //int m = int.Parse(s);//不是拆箱

            //object o = n;//装箱

            //int x = (int)o;//拆箱

            //Console.WriteLine(m);

            //在IL语言中出现 box unbox
            int n = 10;
            object o = n;
            int m = (int)o;
            Console.WriteLine(m);
            Console.ReadKey();
        }
    }
 class Program
    {
        static void Main(string[] args)
        {
            //Chinese ch1 = new Chinese();
            //Person p = ch1;//没有发生装箱,因为都是引用类型
            //Chinese ch2 =(Chinese)p;

            //double d = 999.9;
            //object o = d;
            //double d1 = (double)o;//装箱
            //Console.WriteLine(d1);//拆箱,输出999.9
            //Console.ReadKey();

          
            //int d = 999;
            //object o = d;
            //double d1 = (double)o;//装箱
            //Console.WriteLine(d1);//发生异常,装箱时整型拆箱也必须是整型
            //Console.ReadKey();

            //int n = 10;
            //IComparable com = n;//发生了装箱 接口都是引用类型
            //int m = (int)com;
            //Console.WriteLine(m);
            //Console.ReadKey();

            int n = 10;
            Console.WriteLine(n);
            object o = n; //装箱
            Console.WriteLine((int)o);//拆箱
            Console.WriteLine(o);
            Console.ReadKey();
        }

    }
    public class Person
    {
        public string Name
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }

    public class Chinese : Person
    {
   
    }
    class Program
    {
        static void Main(string[] args)
        {
            int n = 100;
            M1(n);    //调用void M1(double d)  没有去装箱
            string s1 = "a";
            double d1 = 10;
            string s2 = "b";
            int n1 = 9;
            string full = s1 + d1 + s2 + n1;// 装箱两次,d1 和 n1
            Console.WriteLine(full);            
            Console.ReadKey();
        }

        private static void M1(double d)
        {
            Console.WriteLine(d);
        }

        private static void M1(object o)
        {
            Console.WriteLine(o);
        }

    }
   class Program
    {
        static void Main(string[] args)
        {
            int n = 10;
            object o = n;//装箱一次
            n = 100;
            Console.WriteLine(n+","+(int)o);  //装了两次 拆了一次          
            Console.ReadKey();
        }
    } 
  class Program
    {
        static void Main(string[] args)
        {
            //ArrayList arriList = new ArrayList();
            //Stopwatch watch = new Stopwatch();
            //watch.Start();
            //for (int i = 0; i < 1000000; i++)
            //{
            //    arriList.Add(i);
            //}
            //watch.Stop();
            //Console.WriteLine(watch.Elapsed);//00:00:00.99816
            //Console.ReadKey();

            List<int> arriList = new List<int>();
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < 1000000; i++)
            {
                arriList.Add(i);
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);//00:00:00.014241
            Console.ReadKey();
        }
    } 

var类型推断:

 

.Net基础加强06,布布扣,bubuko.com

.Net基础加强06

标签:style   blog   color   os   io   for   ar   art   

原文地址:http://www.cnblogs.com/yechangzhong-826217795/p/3920141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!