简体   繁体   English

C#运算符重载:Object.Equals(object o)和Object.GetHashCode()

[英]C# operator overloading: Object.Equals(object o) & Object.GetHashCode()

So I am creating a BST and want it to be a generic tree, right now I am developing the Node<T> class. 因此,我正在创建一个BST,并希望它成为一棵通用树,现在我正在开发Node<T>类。 I need some help with some of the operator overloading specifics. 我需要一些运算符重载细节方面的帮助。 Class is below: 班级如下:

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;
        }
    }
}

But Visual Studio (and sources I have seen online) say I need to overload the Object.Equals(object o) method and also the Object.GetHashCode. 但是Visual Studio(以及我在网上看到的资源)说,我需要重载Object.Equals(object o)方法以及Object.GetHashCode。

From what I know about .Equals just through using C#, it's like value type semantics, or will compare the values of 2 objects, instead of their references, aka checking if they are actually the same object, which I think is what == usually does when comparing objects. 据我所知, .Equals使用C#就像值类型语义一样,或者将比较2个对象的值,而不是它们的引用,也就是检查它们是否实际上是同一对象,我认为这通常是==比较对象时执行。 So this is what I tried to do: 所以这就是我试图做的:

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

It's basically the same as my == operator, but it doesn't work because the lhs/rhs parameters don't exist. 它与我的==运算符基本相同,但是由于lhs / rhs参数不存在而无法使用。 I have no idea how do accomplish this for my case since how I believe it will be called is n1.Equals(n2) and will check to see if the data values are the same for the nodes. 我不知道如何为我的情况完成此操作,因为我相信它将如何被调用为n1.Equals(n2) ,并将检查节点的数据值是否相同。 Examples online are unclear to me. 在线示例对我来说还不清楚。 I also have no idea why I have to involve this hash method at all, but I am still trying to do research on that. 我也不知道为什么我必须完全使用这种哈希方法,但是我仍在尝试对此进行研究。 Mostly curious about the Equals override for now. 目前,大多数人对Equals替代感到好奇。

Make sure you implement the Equals method according to the MS guide. 确保根据MS指南实施Equals方法。 Here is my snippet. 这是我的片段。 I use all the time: 我一直在用:

        // 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();
    }

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

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