C#学习笔记 IEquatable 接口 IEqualityComparer 接口

C#学习笔记 IEquatable 接口 IEqualityComparer 接口
原创yayakoo- 最后发布于2017-04-02 11:45:45 阅读数 4239  收藏
展开
IEquatable<T> 接口类内重写Equals方法,就可以实现比较。IEqualityComparer<T> 接口可以写一个自定义类的比较类。可以重写GetHashCode()方法。

using System;
using System.Collections.Generic;
using System.Collections;
 
namespace Wrox
{
    public class MainEntryPoint
    {
        static int Main(string[] args)
        {
            var janet = new Person{ FirstName = "Janet",LastName = "Jackson"};
            Person[] persons1 = {
                new Person
                {
                    FirstName = "Michael",
                    LastName = "Jackson"
                },
                janet
            };
            Person[] persons2 = {
                new Person
                {
                    FirstName = "Michael",
                    LastName = "Jackson"
                },
                janet
            };
            if(persons1 != persons2)
                Console.WriteLine("not the same reference.");
            /*if((persons1 as IstructuralEquatable).Equals(persons2,EqualityComparer<Person>.Default))
            {
                Console.WriteLine("the same content.");
            }*/
            var t1 = Tuple.Create<int, string>(1,"Stephanie");
            var t2 = Tuple.Create<int, string>(1,"Stephanie");
            if(t1 != t2)
                Console.WriteLine("not the same reference to the tuple.");
            if(t1.Equals(t2))
                Console.WriteLine("the same content.");
            /*if(t1.Equals(t2,new TupleComparer()))
                Console.WriteLine("equals using TupleComparer.");*/
            return 0;
        }
    }
    public class Person:IEquatable<Person>
    {
        public int Id { get; set;}
        public string FirstName { get; set;}
        public string LastName { get; set;}
 
        public override string ToString()
        {
            return string.Format("{0} {1} {2}",Id,FirstName,LastName);
        }
        public override bool Equals(object obj)//覆盖基类中的Equals方法
        {
            if(obj == null)
                return base.Equals(obj);
            return Equals(obj as Person);
        }
        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
        public bool Equals(Person other)//自定义的Equals方法
        {
            if(other == null)
                return base.Equals(other);
            return this.Id == other.Id && this.FirstName == other.FirstName && this.LastName == other.LastName;
        }
    }
    public class TupleComparer<T1,T2>:IEqualityComparer
    {
        new public bool Equals(object x, object y)
        {
            if (x is T1)
             //if (typeof(x) is string) 
                return true;
            return x.Equals(y);
        }
        public int GetHashCode(object obj)
        {
            if (obj is T1)
                return ((T1) obj).GetHashCode();
            else
                return ((T2) obj).GetHashCode();
            //return obj.GetHashCode();
        }
    }
}

————————————————
版权声明:本文为CSDN博主「yayakoo-」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/july_yeye/article/details/68951425

发布了17 篇原创文章 · 获赞 223 · 访问量 27万+

猜你喜欢

转载自blog.csdn.net/cxu123321/article/details/104856642