0%

C# 判断两对象是否相同 IEquatable接口的实现

很多对象如List, ObservableCollection,在删除对象时,都需要该类实现IEquatable接口中的Equals方法,以判断是否符合条件. Equals方法很简单,只需要判断两者所有的变量,属性值是否相同即可.下面是帮助文档中的例子:

    public class Box : IEquatable
    {

        public Box(int h, int l, int w)
        {
            this.Height = h;
            this.Length = l;
            this.Width = w;
        }
        public int Height { get; set; }
        public int Length { get; set; }
        public int Width { get; set; }

        public bool Equals(Box other)
        {
            if (this.Height == other.Height & this.Length == other.Length
                & this.Width == other.Width)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }