码迷,mamicode.com
首页 > 其他好文 > 详细

自定义泛型类,本质就是利用微软提供的集合和集合接口

时间:2014-07-27 23:27:49      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:des   style   使用   io   for   cti   ar   line   

//实现IEnumerable<A>接口中的GetEnumerator()方法,为了能支持foreach遍历
    class MyClass<A>:IEnumerable<A>
    {
        List<A> list = new List<A>();

        private List<A> items;
        public List<A> Items
        {
            get { return list;}
        }

        //元素个数
        private int count;
        public int Count
        {
            get { return list.Count; }
            set { count = value; }
        }

        public void Add(A item)
        {
            this.list.Add(item);
        }

        public bool Remove(A item)
        {
            return this.list.Remove(item);
        }

        public void RemoveAt(int index)
        {
            this.list.RemoveAt(index);
        }

        #region IEnumerable<A> 成员

        public IEnumerator<A> GetEnumerator()
        {
            return list.GetEnumerator();
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }

 

/***********自定义泛型类****************/
        static void Main(string[] args)
        {
            MyClass<Student> myClass = new MyClass<Student>();
            Student stu1 = new Student() { StuName = "张三", StuNo = "S001", StuAge = 23 };
            Student stu2 = new Student() { StuName = "李四", StuNo = "S002", StuAge = 24 };
            myClass.Add(stu1);
            myClass.Add(stu2);
            Console.WriteLine(myClass.Count);
            foreach (Student item in myClass)
            {
                Console.WriteLine(item.StuName);
            }
        }

  //使用
        /***********排序****************/
        //static void Main(string[] args)
        //{
        //    //整型、字符串可以比较大小,所以能排序
        //    //ArrayList arr = new ArrayList();
        //    //arr.Add("aa");
        //    //arr.Add("ad");
        //    //arr.Add("ac");
        //    //arr.Add("aw");
        //    //arr.Add("ad");
        //    //foreach (var item in arr)
        //    //{
        //    //    Console.WriteLine(item);
        //    //}
        //    //arr.Sort();//排序
        //    //Console.WriteLine("排序后:");
        //    //foreach (var item in arr)
        //    //{
        //    //    Console.WriteLine(item);
        //    //}
        //    //类
        //    List<Student> arr = new List<Student>();
        //    Student stu1 = new Student() { StuName = "张三", StuNo = "S001", StuAge = 23 };
        //    Student stu2 = new Student() { StuName = "李四", StuNo = "S002", StuAge = 14 };
        //    Student stu3 = new Student() { StuName = "王五", StuNo = "S003", StuAge = 28 };
        //    Student stu4 = new Student() { StuName = "赵六", StuNo = "S004", StuAge = 26 };
        //    arr.Add(stu1);
        //    arr.Add(stu4);
        //    arr.Add(stu3);
        //    arr.Add(stu2);          
        //    foreach (var item in arr)
        //    {
        //        Console.WriteLine(item.StuNo + "-" + item.StuName + "-" + item.StuAge);
        //    }
        //    //arr.Sort();//排序(实现接口)
        //    arr.Sort(new AgeComparerDESC());//排序(比较器)
        //    Console.WriteLine("排序后:");
        //    foreach (var item in arr)
        //    {
        //        Console.WriteLine(item.StuNo + "-" + item.StuName + "-" + item.StuAge);
        //    }
        //}

 

自定义泛型类,本质就是利用微软提供的集合和集合接口,布布扣,bubuko.com

自定义泛型类,本质就是利用微软提供的集合和集合接口

标签:des   style   使用   io   for   cti   ar   line   

原文地址:http://www.cnblogs.com/danmao/p/3871937.html

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