Birdman
Birdman

Reputation: 1524

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. 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.

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. 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. 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. 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.

Upvotes: 0

Views: 305

Answers (1)

Marco
Marco

Reputation: 1016

Make sure you implement the Equals method according to the MS guide. 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();
    }

Upvotes: 0

Related Questions