[C#] custom class keys in a dictionary, overloading == etc.

Hello :slight_smile:

I made a custom class iVector2 which is just a Vector2 but with ints instead of floats. I know a bit about overloading operators, but just the super basic basics.

I’m using a dictionary, the keys of which are iVector2s. I got an error “key not present in dictionary” somewhere where I was sure it shouldn’t appear. I assumed it was because I didn’t overload an operator ==. But once I did this I got the following warnings:

`iVector2’ defines operator == or operator != but does not override Object.Equals(object o)

`iVector2’ defines operator == or operator != but does not override Object.GetHashCode()

Ok, so first of all I’d like to know if my reasoning was correct and this error with the keys could have been caused by this or not. I’d also like to know how to get rid of those warnings and what this Object.Equals is. Does it have something to do with interfaces like IComparable (which I know nothing about)?

Thank you very much, have a nice day.

Code:

    public int x;
	public int y;

	public iVector2()	{
		x = 0;
		y = 0;
    }


	public static iVector2 operator ==(iVector2 i1, iVector2 i2) {
	return ((i1.x == iVector2.x) && (i1.y == i2.y));
    }

Should return a bool. Also don’t need a static inside the function.

public static bool operator ==(iVector2 i1, iVector2 i2) {
    return ((i1.x == i2.x) && (i1.y == i2.y));
}

http://www.dotnetperls.com/operator

If you’re writing this code for practice, then that’s fine. But if you’re trying to optimize the current vectors or something for your project, I would recommend that you don’t and just use the Unity vectors as-is.

The following is more or less the standard way:

    public override bool Equals(object o)
    {
        if (o is iVector2)
            return Equals((iVector2)o);
        else
            return base.Equals(o);
    }

    public bool Equals(iVector2 vec)
    {
        if (vec == null)
            return false;
        if (this.x == vec.x && this.y == vec.y)
            return true;
        else
            return false;
    }

    public static bool operator ==(iVector2 v1, iVector2 v2)
    {
        if (Object.ReferenceEquals(v1, v2))
        {
            return true;
        }
        if (v1 == null || v2 == null)
        {
            return false;
        }
    }

    public static bool operator !=(iVector2 v1, iVector2 v2)
    {
        if (Object.ReferenceEquals(v1, v2))
        {
            return false;
        }

        if (v1 == null || v2 == null)
        {
            return true;
        }

        return !((v1.x == v2.x) && (v1.y == v2.y));
    }

    // this implementation is not necessary
    // Only the override is
    public override int GetHashCode()
    {
            int hash = 17;
            // Suitable nullity checks etc, of course :)
            hash = hash * 23 + x.GetHashCode();
            hash = hash * 23 + y.GetHashCode();
            return hash;
    }

C# requires that if you override the equal sign then it wants some other override to secure that you would not misuse the opposite. This is how the language is made.

Now for your dictionary issue, you would have to show the code you use to fill the dictionary (where you do dict.Add(key, value):wink: