IEquatable<T>、IEnumerable<T>

  • IEquatable<T>接口(自定义对象比较)

 class Program
    {
        static void Main(string[] args)
        {
            AnimalA zaa = new AnimalA("zhangsan", 1);
            AnimalA lia = new AnimalA("zhangsan", 1);
            Console.WriteLine($" za.Equals(li):{ zaa.Equals(lia)}"); 

            Console.WriteLine("IEquatable<T>----------");
            Animal za = new Animal("zhangsan", 1);
            Animal li = new Animal("lisi", 1);
            Console.WriteLine($" za.Equals(li):{ za.Equals(li)}");

        }
    }
    class Animal:IEquatable<Animal>
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Animal(string name,int age)
        {
            Name = name;
            Age = age;
        }

        public bool Equals(Animal other)
        {
            return this.Age == other.Age;
        }
    }
    class AnimalA
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public AnimalA(string name, int age)
        {
            Name = name;
            Age = age;
        }
         
    }

  • IEnumerable<T>(自定义迭代对象)

    class Program
    {
        static void Main(string[] args)
        {
            OneClass oneClass = new OneClass();
            foreach (Student item in oneClass)
            {
                item.Description();
            }
        }
    }
    class OneClass:IEnumerable<Student>
    {
        List<Student> students;
        public OneClass()
        {
            students = new List<Student>()
            {
                 new Student("zhangsan",''),
                 new Student("lisi",''),
                 new Student("wangwu",''),
                 new Student("zhaoliu",''),
                 new Student("sunqi",''),
                 new Student("laoba",'')
            };
        }

        public IEnumerator<Student> GetEnumerator()
        {
            return students.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    } 
    class Student
    {
        public string Name { get; set; }
        public char Gender { get; set; }
        private byte Age;
        public Student(string name,char gender)
        {
            Name = name;
            Gender = gender;
            Age = (byte)new Random().Next(1, 20);
        } 
        public void Description()
        {
            Console.WriteLine($"I'm {Name}.{Gender} {Age} years old");
        } 
    }
  • IEnumerable示例:

 static void Main(string[] args)
        {
            IEnumerable<int> ies;
            Console.WriteLine("\n----------List<int> ------------");
            ies = GetCollection(1);
            foreach (var item in ies) 
                Console.Write(item+" ");

            Console.WriteLine("\n----------Queue<int>  ------------");
            ies = GetCollection(2);
            foreach (var item in ies)
                Console.Write(item + " ");
            Console.WriteLine("\n----------int[]  ------------");
            ies = GetCollection(3);
            foreach (var item in ies)
                Console.Write(item + " ");
        }
        static IEnumerable<int> GetCollection(int option)
        {
            List<int> ls = new List<int> { 1, 2, 3 };
            Queue<int> qs = new Queue<int>();
            qs.Enqueue(4);
            qs.Enqueue(5);
            qs.Enqueue(6);

            if (option == 1)
                return ls;
            else if (option == 2)
                return qs;
            else
                return new int[] { 7, 8, 9 };  
        }

 

posted @ 2022-06-29 10:13  后跳  阅读(40)  评论(0编辑  收藏  举报