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

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

概述当将对象添加到.NET System.Collections.Generic.Dictionary类中时,密钥的哈希码将内部存储并用于后续比较.当哈希码在其初始插入到字典之后变化时,它通常变得“不可访问”,并且当存在检查(即使使用相同的引用)返回false(下面的示例代码)时,也可能使用户感到惊讶. GetHashCode文档说: The GetHashCode method for an obj 当将对象添加到.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# – 是字典破碎还是应该GetHashCode()只能基于不可变的成员?全部内容,希望文章能够帮你解决c# – 是字典破碎还是应该GetHashCode()只能基于不可变的成员?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1236027.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存