加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 17:38:19 所属栏目:百科 来源:网络整理
导读:当将对象添加到.NET System.Collections.Generic.Dictionary类中时,密钥的哈希码将内部存储并用于后续比较.当哈希码在其初始插入到字典之后变化时,它通常变得“不可访问”,并且当存在检查(即使使用相同的引用)返回false(下面的示例代码)时,也可能使用户感到
当将对象添加到.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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读