IComparable的和equals() [英] IComparable and Equals()

查看:184
本文介绍了IComparable的和equals()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类型;否则,哈希表可能无法正常工作。

Types that implement IComparable must override Equals.Types that override Equals must also override GetHashCode; otherwise, Hashtable might not work correctly.

我不太明白这一点。谁能解释。

I didn't quite get it. Can anyone explain.

推荐答案

IComparable的是定义实现类的两个实例可以看作是大于一个接口,少大于或等于另一个。既然你已经在接口的方法来定义平等的,你还需要重写Equals方法(和平等的运营商),以确保从两个结果是一致的。

IComparable is an interface that defines that two instances of the implementing class can be seen as greater than, less than or equal to one another. Since you have defined equality in the methods of that interface you also need to override the Equals method (and equality operator) to ensure the results from the two are consistent.

public class EqualityTest : IComparable<EqualityTest>
{
      public int Value { get; set; }

      public int CompareTo(EqualityTest other)
      {
           return this.Value.CompareTo(other.Value);
      }
}

在上面的例子中我已经实现IComparable的,但不重写等于。如果调用的CompareTo与具有相同的值,它会说有平等之类的两个单独的实例。如果调用相同的两个实例等于它会说他们的的平等,因为它会进行测试,看看他们是否是同一个对象(等号的默认实现)。

In the above example I have implemented IComparable, but not overridden Equals. If you call CompareTo with two separate instances of the class that have the same Value it will say there are equal. If you call Equals with the same two instances it will say they are not equal as it will test to see if they are the same object (the default implementation of Equals).

两个相等的项目应返回相同的散列码(这是用于快速查找用作哈希表中的键的项目),所以如果你重载equals那么你也应该重写GetHashCode()

Two equal items should return the same hash code (which are used for quickly finding items used as keys in hash tables) so if you override Equals then you should also override GetHashCode()

作为一个例子,我刚刚创建下面的类在我的IDE:

As an example I just created the following class in my IDE:

public class EqualityTest
{
     public string A { get; set; }
     public string B { get; set; }
}

和跑ReSharper的乐于助人的生成平等功能说,我想既是和B影响平等。这是它创造的代码:

And ran Resharper's helpful "Generate Equality" function saying that I wanted both A and B to affect equality. This is the code it created:

    public bool Equals(EqualityTest other)
    {
        if (ReferenceEquals(null, other))
        {
            return false;
        }

        if (ReferenceEquals(this, other))
        {
            return true;
        }

        return Equals(other.A, A) && Equals(other.B, B);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }

        if (ReferenceEquals(this, obj))
        {
            return true;
        }

        if (obj.GetType() != typeof(EqualityTest))
        {
            return false;
        }

        return Equals((EqualityTest)obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return ((A != null ? A.GetHashCode() : 0)*397) ^ (B != null ? B.GetHashCode() : 0);
        }
    }

    public static bool operator ==(EqualityTest left, EqualityTest right)
    {
        return Equals(left, right);
    }

    public static bool operator !=(EqualityTest left, EqualityTest right)
    {
        return !Equals(left, right);
    }



所以,如果你要重写等号,那么你也应该上述所有的定义为确保一致性,如果要实现IComparable的话同样适用。

So if you are overriding Equals then you should also define all of the above to ensure consistency, if you are implementing IComparable then the same applies.

这篇关于IComparable的和equals()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆