实现正确的GetHashCode

ste*_*avy 3 c# equality gethashcode

我有以下课程

public class ResourceInfo
{
    public string Id { get; set; }
    public string Url { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

其中包含有关某些资源的信息.现在我需要通过以下方案检查两个这样的资源是否相等(我实现了IEquatable接口)

public class ResourceInfo : IEquatable<ResourceInfo>
{
    public string Id { get; set; }
    public string Url { get; set; }

    public bool Equals(ResourceInfo other)
    {
        if (other == null)
            return false;

        // Try to match by Id
        if (!string.IsNullOrEmpty(Id) && !string.IsNullOrEmpty(other.Id))
        {
            return string.Equals(Id, other.Id, StringComparison.InvariantCultureIgnoreCase); 
        }

        // Match by Url if can`t match by Id
        return string.Equals(Url, other.Url, StringComparison.InvariantCultureIgnoreCase);
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:oneResource.Equals(otherResource).一切都很好.但是一段时间过去了,现在我需要在一些linq查询中使用这样的eqaulity比较.

因此,我需要实现单独的Equality comparer,如下所示:

class ResourceInfoEqualityComparer : IEqualityComparer<ResourceInfo>
{
    public bool Equals(ResourceInfo x, ResourceInfo y)
    {
        if (x == null || y == null)
            return object.Equals(x, y);

        return x.Equals(y);
    }

    public int GetHashCode(ResourceInfo obj)
    {
        if (obj == null)
            return 0;

        return obj.GetHashCode();
    }
}
Run Code Online (Sandbox Code Playgroud)

似乎没问题:它产生一些验证逻辑并使用本机相等比较逻辑.但后来我需要在ResourceInfo类中实现GetHashCode方法,这就是我遇到问题的地方.

我不知道如何在不改变类本身的情况下正确地做到这一点.

乍一看,以下示例可以正常工作

public override int GetHashCode()
{
    // Try to get hashcode from Id
    if(!string.IsNullOrEmpty(Id))
        return Id.GetHashCode();
    // Try to get hashcode from url
    if(!string.IsNullOrEmpty(Url))
        return Url.GetHashCode();

    // Return zero
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但这种实施并不是很好.

GetHashCode应该与Equals方法匹配:如果两个对象相等,那么它们应该具有相同的哈希码,对吧?但我的Equals方法使用两个对象来比较它们.这是用例,您可以在其中查看问题本身:

var resInfo1 = new ResourceInfo()
{
    Id = null,
    Url = "http://res.com/id1"
};
var resInfo2 = new ResourceInfo()
{
    Id = "id1",
    Url = "http://res.com/id1"
};
Run Code Online (Sandbox Code Playgroud)

那么,当我们调用Equals方法时会发生什么:显然它们是相同的,因为Equals方法将尝试通过Id匹配它们并失败,然后它尝试通过Url进行匹配,这里我们有相同的值.如预期.

resInfo1.Equals(resInfo1 ) -> true
Run Code Online (Sandbox Code Playgroud)

但是,如果它们相等,它们应该具有相同的哈希码:

var hash1 = resInfo.GetHashCode(); // -263327347
var hash2 = resInfo.GetHashCode(); // 1511443452

hash1.GetHashCode() == hash2.GetHashCode() -> false
Run Code Online (Sandbox Code Playgroud)

简而言之,问题在于Equals方法通过查看两个不同的对象来决定使用哪个字段进行相等比较,而GetHashCode方法只能访问一个对象.

有没有办法正确实现它或我只需要改变我的类来避免这种情况?

非常感谢.

Jon*_*eet 6

你的平等方法从根本上打破了规范Object.Equals.

特别要考虑:

var x = new ResourceInfo { Id = null, Uri = "a" };
var y = new ResourceInfo { Id = "yz", Uri = "a" };
var z = new ResourceInfo { Id = "yz", Uri = "b" };
Run Code Online (Sandbox Code Playgroud)

在这里,x.Equals(y)这将是真的,并且y.Equals(z)是真的 - 但是x.Equals(z)会是错误的.文档中明确禁止这样做:

  • 如果(x.Equals(y) && y.Equals(z))返回true,则x.Equals(z)返回true.

基本上你需要重新设计.