6

I have the following example:

namespace ComparisonExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var hello1 = new Hello();
            var hello2 = new Hello();

            // calls Hello.Equals
            var compareExplicitly = hello1.Equals(hello2);

            // calls Object.Equals
            var compareWithGenerics = ObjectsEqual<Hello>(hello1, hello2); 
        }

        private static bool ObjectsEqual<TValue>(TValue value1, TValue value2)
        {
            return value1.Equals(value2);
        }
    }

    class Hello : IEquatable<Hello>
    {
        public bool Equals(Hello other)
        {
            return true; // doesn't matter
        }
    }
}

The question is why in the second "Equals" call I'm redirected to Object.Equals instead of Hello.Equals even though I'm specifying the exact type in generic argument?

5
  • Because the signature of Equals is bool Equals(Object other)? You're not even over-riding Equals (and missing the override keyword).
    – user645280
    Commented Sep 6, 2013 at 12:22
  • Not really an answer to your question, but why not just public override bool Equals(object other) { return Equals(other as Hello); }?
    – Corak
    Commented Sep 6, 2013 at 12:23
  • 1
    Because I don't want to have troubles with GetHashCode
    – psurikov
    Commented Sep 6, 2013 at 12:24
  • Troubles with GetHashCode? o_O
    – Corak
    Commented Sep 6, 2013 at 12:25
  • 1
    No constraints for TValue, therefore Equals from Object.
    – Vladimir
    Commented Sep 6, 2013 at 12:25

2 Answers 2

11

Because you haven't told the generic method that your object implements IEquatable<T>:

Try now with:

private static bool ObjectsEqual<TValue>(TValue value1, TValue value2) 
               where TValue : IEquatable<TValue> // IMPORTANT!!!
{
    return value1.Equals(value2);
}

In your ObjectsEqual method you have access only to methods/properties/fields of TValue that are defined in the object class plus the methods that are defined in the interfaces/base classes defined in the constraints. No constraints => you have access only to Equals(object), GetHashCode(), GetType(), (and if you have the constraint class: operator==, operator!=.) Of these two are virtual (Equals(object), GetHashCode()), so you'll use the "correct" version, the third isn't normally overwritten (GetType()), so you'll probably use the "correct" version. Only the two operators ==/!= are often overwritten and lo and behold! In your generic method you can't use the "correct" version of the two! :-)

6
  • Interesting, I'm still getting Object.Equals if I specify where TValue : IEquatable<Hello>. But your answer is correct, where TValue : IEquatable<TValue> does its job
    – psurikov
    Commented Sep 6, 2013 at 12:31
  • 1
    @username I'll add that, in the .NET libraries, when they want to compare generic types, they do EqualityComparer<T>.Default.Equals(T, T)
    – xanatos
    Commented Sep 6, 2013 at 12:39
  • TValue: IEquatable<Hello> means signature bool Equals(Hello other), when in generic you need bool Equals(TValue other).
    – Vladimir
    Commented Sep 6, 2013 at 12:46
  • 1
    @VladimirFrolov Got it, so I need where TValue : Hello, IEquatable<Hello> to make this particular example work.
    – psurikov
    Commented Sep 6, 2013 at 12:50
  • where TValue: IEquatable<TValue> or where TValue: Hello is sufficient.
    – Vladimir
    Commented Sep 6, 2013 at 13:07
2

Addition from MSDN:

Unbounded Type Parameters.
Type parameters that have no constraints, such as T in public class SampleClass<T> { }, are called unbounded type parameters.
Unbounded type parameters have the following rules:

  • The != and == operators cannot be used because there is no guarantee that the concrete type argument will support these operators.
  • They can be converted to and from System.Object or explicitly converted to any interface type.
  • You can compare to null. If an unbounded parameter is compared to null, the comparison will always return false if the type argument is a value type.

In this case TValue is converted to System.Object and Equals method called.

Not the answer you're looking for? Browse other questions tagged or ask your own question.