繁体   English   中英

如何为HashSet覆盖Equals和GetHashCode?

[英]How should I override Equals and GetHashCode for HashSet?

可以说我上课了:

    public class Ident
    {
        public String Name { get; set; }
        public String SName { get; set; }
    }

还有一个:

    class IdenNode
    {
        public Ident id { get; set; }
        public List<IdenNode> Nodes { get; set; }

        public IdenNode()
        {
            Nodes = new List<IdenNode>();
        }
    }

我想使用HashSet<IdenNode>的前提是,当且仅当它们的id.Names相等时,它的两个元素才相同。

因此,我将像下面那样覆盖EqualsGetHashCode

        public override bool Equals(object obj)
        {
            IdenNode otherNode = obj as IdenNode;

            return otherNode != null && 
                   otherNode.id != null && 
                   id.Name == otherNode.id.Name;
        }

        public override int GetHashCode()
        {
            if (id != null)
                return id.Name.GetHashCode();
            else
                // what should I write here?
        }

我认为对吗? 如果是这样,我应该在GetHashCode放置什么?

更新

能告诉我是OK使用==!=Equals方法是什么? 或者是ReferenceEquals或其他?

另外,我应该重写运算符==!=吗?

如果id (或id.Name )为null,则返回0是完全可以的id.Name Nullable<T> (如int? )对于“ null”值返回0。

请记住,两个对象从GetHashCode()返回相同的值并不意味着相等-它仅意味着两个对象可能相等。 然而,相反的情况是,两个“相等”的对象必须返回相同的哈希码。 您对EqualsGetHashCode定义似乎都满足了这两个原则

提防空 你有很多。 请注意StackOverflow不要Equals方法中使用 ==和!=。 通常,在null的情况下,我们返回0作为哈希码,例如:

public override bool Equals(object obj) {
  // Often we should compare an instance with itself, 
  // so let's have a special case for it (optimization)
  if (Object.ReferenceEquals(obj, this)) 
    return true;

  IdenNode other = obj as IdenNode;

  // otherNode != null line in your code can cause StackOverflow:
  // "!=" calls "Equals" which in turn calls "!=" etc...
  if (Object.ReferenceEquals(null, other))
    return false;

  // Id can be null
  if (Object.ReferenceEquals(id, other.id))
    return true;
  else if (Object.ReferenceEquals(id, null) || Object.ReferenceEquals(other.id, null))
    return false;

  // Let's be exact when comparing strings:
  // i.e. should we use current locale or not etc
  return String.Equals(id.Name, other.id.Name, StringComparison.Ordinal);
}

public override int GetHashCode() {
  // It's typical to return 0 in case of null
  if (Object.ReferenceEquals(null, id))
    return 0;
  else if (Object.ReferenceEquals(null, id.Name)) // <- Name can be null as well!
    return 0;

  return id.Name.GetHashCode();
}

如果是这样,我应该在GetHashCode中放置什么?

返回零是可以的。 注意,在名称上定义值相等是一个坏主意; 我知道在美国至少还有其他三个Eric Lippert ,但他们不是我。 实际上,有数百万甚至数十亿人有名字冲突。

请问我可以在Equals方法中使用“ ==”和“!=”吗? 或者是ReferenceEquals或其他?

我的建议是:在混合参考和价值平等时, 要非常清楚 如果您打算引用相等,请这样说。

另外,我应该重写运算符“ ==”和“!=”吗?

是。 Equals等于一件事而==意味着另一件事是令人困惑的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM