如何为由字符串和 int32 集合构成的对象编写 GetHashCode 方法?

How do you write a GetHashCode method for an object made of a string and a collection of int32?

有 class 个产品:

public class ProductWithFeatures
{
    public string Name { get; set; }
    public ICollection<Feature> Features { get; set; }
}

public class Feature
{
    public int Id { get; set; }
    
    public Feature(int Id)
    {
        this.Id = Id;
    }
}

我想为此编写一个 IEqualityComparer(我已经有一个用于功能)。

Feature的那个是这样的:

public class FeatureComparer : IEqualityComparer<Feature>
{
    public bool Equals(Feature x, Feature y)
    {
        return x.Id == y.Id;
    }

    public int GetHashCode(Feature obj)
    {
        return obj.Id;
    }
}

到目前为止我在另一个上写的是这样的:

public class ProductComparer : IEqualityComparer<LinqHomework.ProductWithFeatures>
        {
            public bool Equals(ProductWithFeatures x, ProductWithFeatures y)
            {
                return x.Name == y.Name && LinqHomework.FeatureComparer.Equals(x.Features, y.Features);
            }

            public int GetHashCode(ProductWithFeatures obj)
            {
    
            }
        }

我找不到任何关于这个的答案。有人知道怎么写吗?

这真的取决于你。我个人会选择

public int GetHashCode( ProductWithFeatures obj )
{
    string toHash = obj.Name;
    foreach( var feature in obj.Features )
        toHash += feature.GetHashCode();

    return toHash.GetHashCode();
}

这不是有史以来最好的代码,但它做了它应该做的事情。

如果两个 ProductWithFeatures 具有相同的名称,并且具有相同顺序的相同特征,则它们是相等的。

public class ProductComparer : IEqualityComparer<LinqHomework.ProductWithFeatures>
{
    public bool Equals(ProductWithFeatures x, ProductWithFeatures y)
    {
        return x.Name == y.Name && x.Features.SequenceEqual(y.Features, new LinqHomework.FeatureComparer());
    }

    public int GetHashCode(ProductWithFeatures obj)
    {
        int hash = obj.Name.GetHashCode();
        var featureComparer = new LinqHomework.FeatureComparer();
        foreach (var feature in obj.Features)
        {
            hash = hash * 23 + featureComparer.GetHashCode(feature);
        }
        return hash;
    }
}

这是一种简单的方法,可以通过多种方式进行改进。

首先,让我们给我们的 FeatureComparer 一个 Default 属性,这样我们就不需要继续创建新实例了:

public class FeatureComparer : IEqualityComparer<Feature>
{
    public static FeatureComparer Default { get; } = new FeatureComparer();
    // ... as before
}

这让我们写:

public class ProductComparer : IEqualityComparer<LinqHomework.ProductWithFeatures>
{
    public bool Equals(ProductWithFeatures x, ProductWithFeatures y)
    {
        return x.Name == y.Name && x.Features.SequenceEqual(y.Features, LinqHomework.FeatureComparer.Default);
    }

    public int GetHashCode(ProductWithFeatures obj)
    {
        int hash = obj.Name.GetHashCode();
        foreach (var feature in obj.Features)
        {
            hash = hash * 23 + LinqHomework.FeatureComparer.Default.GetHashCode(feature);
        }
        return hash;
    }
}

我们也不会处理我们的方法被传递 null 或功能名称是 null 的情况,所以让我们来处理这些情况。我们还可以测试 xy 是否是 Equals.

中的同一个对象

我们还将在 unchecked 块中进行整数运算,以防它溢出(并且程序集是使用 /checked 编译的)。

请注意,我们使用 ReferenceEquals 而不是 ==,以防您最终在您的类型中实现 == 运算符。

public class ProductComparer : IEqualityComparer<LinqHomework.ProductWithFeatures>
{
    public bool Equals(ProductWithFeatures x, ProductWithFeatures y)
    {
        if (ReferenceEquals(x, y))
            return true;
        if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
            return false;

        if (x.Name != y.Name)
            return false;

        if (ReferenceEquals(x.Features, y.Features))
            return true;
        if (ReferenceEquals(x.Features, null) || ReferenceEquals(y.Features, null))
            return false;
        if (!x.Features.SequenceEquals(y.Features, LinqHomework.FeatureComparer.Default))
            return false;

        return true;
    }

    public int GetHashCode(ProductWithFeatures obj)
    {
        if (ReferenceEquals(obj, null))
            return 0;

        unchecked
        {
            int hash = obj.Name?.GetHashCode() ?? 0;
            if (!ReferenceEquals(obj.Features, null))
            {
                foreach (var feature in obj.Features)
                {
                    hash = hash * 23 + LinqHomework.FeatureComparer.Default.GetHashCode(feature);
                }
                return hash;
            }
        }
    }
}