【问题标题】:C# operator overloading: Object.Equals(object o) & Object.GetHashCode()C# 运算符重载:Object.Equals(object o) & Object.GetHashCode()
【发布时间】:2018-02-21 06:05:15
【问题描述】:

所以我正在创建一个 BST 并希望它成为一个通用树,现在我正在开发 Node<T> 类。我需要一些关于运算符重载细节的帮助。类如下:

public class Node<T> where T : IComparable
{
    //Private member data
    private T data;
    private Node<T> left;
    private Node<T> right;
    //private readonly IComparer<T> _comparer;

    //Node constructor
    public Node()
    {
        data = default(T); //
        left = null;
        right = null;
    }

    //Setters/getters for node private data members
    public T Data
    {
        get { return data; }
        set { data = value; }
    }

    public Node<T> Left
    {
        get { return left; }
        set { left = value; }
    }

    public Node<T> Right
    {
        get { return right; }
        set { right = value; }
    }

    public static bool operator ==(Node<T> lhs, Node<T> rhs)
    {
        if((lhs.Data).CompareTo(rhs.Data) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }

    }

    public static bool operator !=(Node<T> lhs, Node<T> rhs)
    {
        if (lhs.Data.CompareTo(rhs.Data) != 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

但 Visual Studio(以及我在网上看到的资源)说我需要重载 Object.Equals(object o) 方法以及 Object.GetHashCode。

根据我对 .Equals 的了解,仅通过使用 C#,它就像值类型语义,或者将比较 2 个对象的值,而不是它们的引用,也就是检查它们是否实际上是同一个对象,我认为是== 在比较对象时通常会做什么。所以这就是我试图做的:

public override bool Equals(Object obj)
{
    if ((lhs.Data).CompareTo(rhs.Data) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

和我的==操作符基本一样,但是因为lhs/rhs参数不存在所以不行。我不知道如何为我的案例完成此操作,因为我相信它将被称为 n1.Equals(n2) 并将检查节点的数据值是否相同。网上的例子我不清楚。我也不知道为什么我必须使用这种哈希方法,但我仍在尝试对此进行研究。目前主要对 Equals 覆盖感到好奇。

【问题讨论】:

  • 您很可能只覆盖 Equals(而不是重载 ==),然后代码将是 =&gt; (obj is Node&lt;T&gt; otherNode) &amp;&amp; (Data.CompareTo(otherNode.Data) == 0)
  • @vc74 哦,好吧,所以我想我不明白的原因是因为我没有得到“数据”的来源,但我想那是调用这个重载方法的节点?所以如果是:n1.Equals(n2);那么Data来自n1,显然obj是n2?
  • 是的,Equals 将此实例与另一个实例进行比较。要完全实现该模式,您可以实现IEquatable&lt;Node&lt;T&gt;&gt;(不要忘记覆盖GetHashCode)
  • 'this' 是 lhs 而 'obj'(在检查它的类型是否正确后)是 rhs

标签: c# operator-overloading binary-search-tree


【解决方案1】:

确保根据 MS 指南实施 Equals 方法。这是我的sn-p。我一直在使用:

        // override object.Equals
public override bool Equals(object obj)
    {
        // shortcut
        if (object.ReferenceEquals(this, obj))
        {
            return true;
        }

        // check for null and make sure we do not break oop / inheritance
        if (obj == null || GetType() != obj.GetType())
        {
            return false;
        }

        // TODO: write your implementation of Equals() here
        // do not call base.equals !        
        throw new NotImplementedException();

        return false;
    }

    public static bool operator ==(Class lobj, Class robj)
    {
        // users expect == working the same way as Equals
        return object.Equals(lobj, robj);
    }

    public static bool operator !=(Class lobj, Class robj)
    {
        return !object.Equals(lobj, robj);
    }

    // override object.GetHashCode
    public override int GetHashCode()
    {
        // TODO: write your implementation of GetHashCode() here
        // return field.GetHashCode() ^ field2.GetHashCode() ^ base.GetHashCode();
        // or simply return the unique id if any

        throw new NotImplementedException();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 2019-06-05
    • 2016-03-17
    • 1970-01-01
    相关资源
    最近更新 更多