c# – 是字典破碎还是应该GetHashCode()只能基于不可变的成员?

前端之家收集整理的这篇文章主要介绍了c# – 是字典破碎还是应该GetHashCode()只能基于不可变的成员?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当将对象添加到.NET System.Collections.Generic.Dictionary类中时,密钥的哈希码将内部存储并用于后续比较.当哈希码在其初始插入到字典之后变化时,它通常变得“不可访问”,并且当存在检查(即使使用相同的引用)返回false(下面的示例代码)时,也可能使用户感到惊讶.

GetHashCode文档说:

The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object’s Equals method.

因此,根据GetHashCode文档,当equality确定状态改变时,哈希码可能会更改,但Dictionary实现不支持此操作.

当前的.NET字典实现是否被破坏,因为它错误地忽略了哈希码容限? GetHashCode()只能基于不可变成员?还有还有什么可以打破可能的假二分法?

class Hashable
{
    public int PK { get; set; }

    public override int GetHashCode()
    {
        if (PK != 0) return PK.GetHashCode();
        return base.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as Hashable);
    }

    public virtual bool Equals(Hashable other)
    {
        if (other == null) return false;
        else if (ReferenceEquals(this,other)) return true;
        else if (PK != 0 && other.PK != 0) return Equals(PK,other.PK);
        return false;
    }

    public override string ToString()
    {
        return string.Format("Hashable {0}",PK);
    }
}

class Test
{
    static void Main(string[] args)
    {
        var dict = new Dictionary<Hashable,bool>();
        var h = new Hashable();
        dict.Add(h,true);

        h.PK = 42;
        if (!dict.ContainsKey(h)) // returns false,despite same reference
            dict.Add(h,false);
    }
}

解决方法

不,在插入字典之后,你不应该将键(以实质的方式)变异.这是通过设计,以及我使用过的每个散列表的工作原理.文档甚至指定:

As long as an object is used as a key in the Dictionary<TKey,TValue>,it must not change in any way that affects its hash value. Every key in a Dictionary<TKey,TValue> must be unique according to the dictionary’s equality comparer. A key cannot be null,but a value can be,if the value type TValue is a reference type.

所以只会让用户看不到文档的惊喜:)

猜你在找的C#相关文章