我重写了我的类之一的Equals方法。在该方法中,我检查每对字典的与另一个实例的字典中的平等,像下面这样做GetHashCode for Dictionary项目
public override bool Equals (object obj)
{
...
// compare to make sure all <key, value> pair of this.dict have
// the match in obj.dict
...
}
现在,我需要重写GetHashCode方法以及建议的内容。
我是否需要为字典的所有键或键和值添加值?
基本上,以下是好还是矫枉过正?
public override int GetHashCode()
{
int iHash = 0;
foreach (KeyValuePair<string, T> pair in this.dict)
{
iHash ^= pair.Key.GetHashCode();
iHash ^= pair.Value.GetHashCode();
}
return iHash;
}
http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overriden-in-c –
http://stackoverflow.com/questions/1378686/general-advice-and-guidelines-how-to-properly-override-object-gethashcode –
你在为什么生成哈希码?这是您创建的自定义类吗?或者你是否将一个IEqualityComparer传递给现有的字典? –