C# 集合 转换 比较 迭代器 索引及深拷贝

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qiqingli/article/details/79262804
 public class chp11
    {

        public void Test2()
        {
            /* 11 章 集合 比较和转换 知识要点
             * 1 自定义集合:集合是可以包含其他类的实例的类。要定义集合,可以从CollectionBase中派生
             *  ,或者自己实现接口,例如IEnumerable,ICollection 和IList接口。一般需要为集合定义一个
             *  索引器,以实用collection[index]语法来访问集合成员。
             *  
             * 2 字典,也可以定关键字值集合,即字典,字典中的每一项都有一个关联的键。在字典中,键可以
             *  用于标识一项,而无需使用该项的索引。定义自定时,可以实现IDictionary或者从DictionaryBase
             *  中派生类
             * 
             * 3 迭代器,可以实现一个迭代器,来控制循环代码如何在循环中取值。要迭代一个类,需要实现
             *  GetEnumerator()方法,其返回类型是IEnumerator。要迭代类的成员,例如方法,可以使用IEnumberable
             *  返回类型。在迭代器的代码块中,使用yield关键字返回值。
             * 
             * 4 类型比较,使用GetType()方法可以获得对象的类型,使用typeof()运算符可以获得类的类型。可以比较
             *  这些类型值,还可以使用is运算符确定对象是否与某个类类型兼容。
             *  
             * 5 值比较,如果希望类的实例可以用标准的C#运算符进行比较,就必须在类定义中重载这些运算符。对于其他了
             *  型的值比较,可以使用实现了IComparable或IComparer接口的类。这些接口特别适用于集合的排序。
             * 
             * 6 as运算符,可以使用as运算符把一个值转换为引用类型。如果不能进行转换,as运算符就返回null值。
             * 
             */
            Person A = new Person("A", 11);
            Person B = new Person("B", 18);
            Person C = new Person("C", 21);
            Person D = new Person("D", 19);
            Person E = new Person("E", 10);
            Person F = new Person("F", 17);
            Person G = new Person("G", 21);
            People people1 = new People();
            people1.Add(A);
            people1.Add(B);
            people1.Add(C);
            people1.Add(D);
            people1.Add(E);
            people1.Add(F);
            people1.Add(G);
            foreach (DictionaryEntry p in people1)
            {
                var p1 = p.Value as Person;
                Console.WriteLine(p1.Name + "   Age:" + p1.Age);
            }
            Console.WriteLine("The oldest Person:");
            foreach (var p in people1.GetOldest())
            {
                var p1 = p as Person;
                Console.WriteLine(p1.Name + "   Age:" + p1.Age);
            }

            Console.WriteLine("The index of C Person:people1['C']");
            var per = people1["C"];

            Console.WriteLine("Name:" + per.Name + "  Age:" + per.Age);

            Console.WriteLine("IEnumberator Ages:");
            foreach (var p in people1.Ages)
            {
                Console.WriteLine(p);
            }
            ArrayList lstperson = new ArrayList();
            lstperson.Add(A);
            lstperson.Add(B);
            lstperson.Add(C);
            lstperson.Add(D);
            lstperson.Add(E);
            lstperson.Add(F);
            lstperson.Add(G);
            Console.WriteLine("The Sort By Age:");
            lstperson.Sort();//默认的CompareTo,age
            foreach (var p in lstperson)
            {
                var p1 = p as Person;
                Console.WriteLine(p1.Name + "   Age:" + p1.Age);
            }
            Console.WriteLine("The Sort By Name:");
            lstperson.Sort(PersonCompareName.Defalut);//Compare  Name
            foreach (var p in lstperson)
            {
                var p1 = p as Person;
                Console.WriteLine(p1.Name + "   Age:" + p1.Age);
            }
        }
    }

    /// <summary>
    /// 实现IConeable 可以深复制;
    /// IComparable,可以比较
    /// </summary>
    public class Person : ICloneable, IComparable
    {
        private string name;
        private int age;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        private Person() : this("NoName", 0)
        {
        }
        public Person(string _name, int _age)
        {
            name = _name;
            age = _age;
        }
        public static bool operator >(Person p1, Person p2)
        {
            return p1.age > p2.age;
        }
        public static bool operator <(Person p1, Person p2)
        {
            return p1.age < p2.age;
        }
        public static bool operator >=(Person p1, Person p2)
        {
            return !(p1 < p2);
        }
        public static bool operator <=(Person p1, Person p2)
        {
            return !(p1 > p2);
        }

        public object Clone()
        {
            Person newperson = new Person();
            newperson.name = this.name;
            newperson.age = this.age;
            return newperson;
        }

        public int CompareTo(object obj)
        {
            if (obj is Person)
            {
                return this.Age - (obj as Person).Age;
            }
            else
            {
                throw new ArgumentException("obj is not person");
            }
        }
    }

    public class PersonCompareName : IComparer
    {
        public static IComparer Defalut = new PersonCompareName();
        public int Compare(object x, object y)
        {
            if (x is Person && y is Person)
            {
                return Comparer.Default.Compare(((Person)x).Name, ((Person)y).Name);
            }
            else
            {
                throw new ArgumentException("Type is not same");
            }
        }
    }
    /// <summary>
    /// 继承自DictionaryBase,集合;
    /// </summary>
    public class People : DictionaryBase, ICloneable
    {
        public void Add(Person p)
        {
            Dictionary.Add(p.Name, p);
        }
        public void Remove(string name)
        {
            Dictionary.Remove(name);
        }
        /// <summary>
        /// 索引器
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Person this[string name]
        {
            get { return Dictionary[name] as Person; }
        }
        public Person[] GetOldest()
        {
            Person oldper = null;
            People oldpel = new People();
            Person curPer;
            foreach (DictionaryEntry p in Dictionary)
            {
                curPer = p.Value as Person;
                if (oldper == null)
                {
                    oldper = curPer;
                    oldpel.Add(oldper);
                }
                else
                {
                    if (curPer > oldper)
                    {
                        oldpel.Clear();
                        oldpel.Add(curPer);
                        oldper = curPer;
                    }
                    else
                    {
                        if (curPer >= oldper)
                        {
                            oldpel.Add(curPer);
                        }
                    }
                }
            }
            Person[] oldestPeopleArray = new Person[oldpel.Count];
            int copyIndex = 0;
            foreach (DictionaryEntry p in oldpel)
            {
                oldestPeopleArray[copyIndex] = p.Value as Person;
                copyIndex++;
            }
            return oldestPeopleArray;
        }
        /// <summary>
        /// 深复制
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            People clonedPeople = new People();
            Person currentPerson;
            foreach (DictionaryEntry p in Dictionary)
            {
                currentPerson = p.Value as Person;
                clonedPeople.Add(currentPerson.Clone() as Person);
            }
            return clonedPeople;

        }

        /// <summary>
        /// 迭代器,按照Age循环
        /// </summary>
        public IEnumerable Ages
        {
            get
            {
                foreach (DictionaryEntry p in Dictionary)
                {
                    yield return (p.Value as Person).Age;
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/qiqingli/article/details/79262804