When I compare an Object (type) does it uses the IEquatable of a specific class?

325 views Asked by At

My method receives two parameters, both of Object type. They have the same type, that implements IEquatable.

My question is: when I do: param1 == param2 does the framework compare using the IEquatable operator override of specific class or does it uses the object.Equals that just compares the memory pointer of two objects?

Which is the best way to do this? Is with generics and derivation constraints?

3

There are 3 answers

3
Dan Tao On BEST ANSWER

Actually, it does neither. The == operator by default will test for reference equality, regardless of the overridden behavior of your Equals method (if you've overridden it, which you certainly should have if you implemented IEquatable<T>).

That is to say, if your variables are typed as object but you want to use your own custom equality comparison, use Equals(x, y) rather than x == y.

Then, even if you've implemented IEquatable<T>, be sure to still override object.Equals, like this:

class MyType : IEquatable<MyType>
{
    public bool Equals(MyType other)
    {
        // Whatever you want.
    }

    public override bool Equals(object other)
    {
        // Presumably you check for null above.
        return Equals(other as MyType);
    }
}

While you certainly can also overload the == and != operators for your type, this won't accomplish anything if you have references to objects of this type that are simply object variables, like this:

object x = new MyType();
object y = new MyType();
Console.WriteLine(Equals(x, y));
Console.WriteLine(x == y);

The above won't work as you might expect (if you've overloaded == and expect that to be used) because the == overload has to be resolved at compile-time; since x and y are typed as arbitrary objects, the C# compiler will pick the object type's == operator, which, again, just tests for reference equality.


Update: Now, you can ensure your == operator is used if your variables are typed as the class wherein you defined it or a more derived type. For example, given the following types:

class A
{
    public static bool operator ==(A x, A y) { return true; }
    public static bool operator !=(A x, A b) { return false; }
}

class B : A { }

class AComparer<T> where T : A
{
    public bool CompareEqual(T x, T y) { return x == y; }
}

The AComparer<T>.CompareEqual method above will use your overloaded == operator for any type T deriving from A.

The key thing to remember is that == is static, which means its overload resolution gets performed at compile-time, not at run-time using a vtable (unless you're using dynamic, but that's a whole other beast). So just be aware of that whenever you're using the == operator in code and you want the overload to resolve to that of your custom type.

0
LukeH On

If your method's parameters are specified as object then doing param1 == param2 just performs reference equality, since the == operator isn't polymorphic.

0
Felice Pollano On