如何在没有任何数字作为字段的情况下覆盖 GetHashCode()?
How do I override GetHashCode() without any numbers as fields?
显示如何覆盖 Equals(object)
和 GetHashCode()
的所有资源都使用数字字段来实现 GetHashCode()
方法:
Implementing the Equals Method
What's the best strategy for Equals and GetHashCode?
Why is it important to override GetHashCode when Equals method is overridden?
但是,在我的 class 中,我没有任何数字字段。它是树中的一个节点,引用其父节点、子节点和接口作为数据:
public class Node
{
private IInterface myInterface;
private Node parent;
private List<Node> children = new List<Node>();
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
var node = (Node)obj;
return myInterface == node.myInterface;
}
public override int GetHashCode()
{
???
}
}
我应该用什么设置哈希码?
根据 Equals
实现,当且仅当它们的 myInterface
相等时,两个 Node
s 实例才相等:
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
var node = (Node)obj;
// instances are equal if and only if myInterface's are equal
return myInterface == node.myInterface;
}
这就是为什么 myInterface
是 GetHashCode
的唯一来源:
public override int GetHashCode()
{
return null == myInterface ? 0 : myInterface.GetHashCode();
}
P.S.(已编辑,感谢 Kris Vandermotten)通常,检查 ReferenceEquals
在 Equals
实现之前比较可能 time/resource 消耗 myInterface
s:
public override bool Equals(object obj) {
// Easy tests:
// 1. If "this" and "obj" are in fact just the same reference?
// 2. Since `Node` (or Equals) is not sealed, the safiest is to check types
if (object.ReferenceEquals(this, obj))
return true;
else if (null == obj || other.GetType() != GetType())
return false;
// Potentially time/resource cosuming (we don't know IInterface implementation)
return ((Node) obj).myInterface == myInterface;
}
显示如何覆盖 Equals(object)
和 GetHashCode()
的所有资源都使用数字字段来实现 GetHashCode()
方法:
Implementing the Equals Method
What's the best strategy for Equals and GetHashCode?
Why is it important to override GetHashCode when Equals method is overridden?
但是,在我的 class 中,我没有任何数字字段。它是树中的一个节点,引用其父节点、子节点和接口作为数据:
public class Node
{
private IInterface myInterface;
private Node parent;
private List<Node> children = new List<Node>();
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
var node = (Node)obj;
return myInterface == node.myInterface;
}
public override int GetHashCode()
{
???
}
}
我应该用什么设置哈希码?
根据 Equals
实现,当且仅当它们的 myInterface
相等时,两个 Node
s 实例才相等:
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
var node = (Node)obj;
// instances are equal if and only if myInterface's are equal
return myInterface == node.myInterface;
}
这就是为什么 myInterface
是 GetHashCode
的唯一来源:
public override int GetHashCode()
{
return null == myInterface ? 0 : myInterface.GetHashCode();
}
P.S.(已编辑,感谢 Kris Vandermotten)通常,检查 ReferenceEquals
在 Equals
实现之前比较可能 time/resource 消耗 myInterface
s:
public override bool Equals(object obj) {
// Easy tests:
// 1. If "this" and "obj" are in fact just the same reference?
// 2. Since `Node` (or Equals) is not sealed, the safiest is to check types
if (object.ReferenceEquals(this, obj))
return true;
else if (null == obj || other.GetType() != GetType())
return false;
// Potentially time/resource cosuming (we don't know IInterface implementation)
return ((Node) obj).myInterface == myInterface;
}