c# – 是字典破碎还是应该GetHashCode()只能基于不可变的成员?
当将对象添加到.NET
System.Collections.Generic.Dictionary类中时,密钥的哈希码将内部存储并用于后续比较.当哈希码在其初始插入到字典之后变化时,它通常变得“不可访问”,并且当存在检查(即使使用相同的引用)返回false(下面的示例代码)时,也可能使用户感到惊讶.
GetHashCode文档说:
因此,根据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); } } 解决方法
不,在插入字典之后,你不应该将键(以实质的方式)变异.这是通过设计,以及我使用过的每个散列表的工作原理.文档甚至指定:
所以只会让用户看不到文档的惊喜:) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |