GetHashCode覆盖包含泛型数组的对象

我有一个包含以下两个属性的类:

public int Id      { get; private set; }
public T[] Values  { get; private set; }

我已IEquatable<T>和重写所述object.Equals这样的:

public override bool Equals(object obj)
{
    return Equals(obj as SimpleTableRow<T>);
}

public bool Equals(SimpleTableRow<T> other)
{
    // Check for null
    if(ReferenceEquals(other, null))
        return false;

    // Check for same reference
    if(ReferenceEquals(this, other))
        return true;

    // Check for same Id and same Values
    return Id == other.Id && Values.SequenceEqual(other.Values);
}

当重写object.Equals我也必须重写GetHashCode当然。 但是我应该实现哪些代码? 如何从通用数组中创建散列码? 我如何将它与Id整数结合起来?

public override int GetHashCode()
{
    return // What?
}

由于在这个线程中提出的问题,我发布了另一个回复,显示如果你弄错了会发生什么......主要是,你不能使用数组的GetHashCode() ; 正确的行为是在运行时不会打印任何警告......切换注释以修复它:

using System;
using System.Collections.Generic;
using System.Linq;
static class Program
{
    static void Main()
    {
        // first and second are logically equivalent
        SimpleTableRow<int> first = new SimpleTableRow<int>(1, 2, 3, 4, 5, 6),
            second = new SimpleTableRow<int>(1, 2, 3, 4, 5, 6);

        if (first.Equals(second) && first.GetHashCode() != second.GetHashCode())
        { // proven Equals, but GetHashCode() disagrees
            Console.WriteLine("We have a problem");
        }
        HashSet<SimpleTableRow<int>> set = new HashSet<SimpleTableRow<int>>();
        set.Add(first);
        set.Add(second);
        // which confuses anything that uses hash algorithms
        if (set.Count != 1) Console.WriteLine("Yup, very bad indeed");
    }
}
class SimpleTableRow<T> : IEquatable<SimpleTableRow<T>>
{

    public SimpleTableRow(int id, params T[] values) {
        this.Id = id;
        this.Values = values;
    }
    public int Id { get; private set; }
    public T[] Values { get; private set; }

    public override int GetHashCode() // wrong
    {
        return Id.GetHashCode() ^ Values.GetHashCode();
    }
    /*
    public override int GetHashCode() // right
    {
        int hash = Id;
        if (Values != null)
        {
            hash = (hash * 17) + Values.Length;
            foreach (T t in Values)
            {
                hash *= 17;
                if (t != null) hash = hash + t.GetHashCode();
            }
        }
        return hash;
    }
    */
    public override bool Equals(object obj)
    {
        return Equals(obj as SimpleTableRow<T>);
    }
    public bool Equals(SimpleTableRow<T> other)
    {
        // Check for null
        if (ReferenceEquals(other, null))
            return false;

        // Check for same reference
        if (ReferenceEquals(this, other))
            return true;

        // Check for same Id and same Values
        return Id == other.Id && Values.SequenceEqual(other.Values);
    }
}

FWIW,在散列码中使用值的内容是非常危险的。 如果你能保证它永远不会改变,你只应该这样做。 但是,由于它是暴露的,我不认为保证它是可能的。 对象的哈希码不应该改变。 否则,它将失去作为Hashtable或Dictionary中的关键字的价值。 考虑在Hashtable中使用对象作为键的难以发现的错误,其哈希码由于外部影响而改变,并且您无法再在哈希表中找到它!


由于hashCode是存储对象(lllike在哈希表中)的关键,我只会使用Id.GetHashCode()

链接地址: http://www.djcxy.com/p/39779.html

上一篇: GetHashCode override of object containing generic array

下一篇: How to impelement GetHashCode in class without any ID