Custom equality comparer for Type in dictionary

vexe picture vexe · Sep 12, 2014 · Viewed 7.2k times · Source

Since Int32 is a Object, I want this to print "True"

    Dictionary<Type, string> dict = new Dictionary<Type, string>(new MyComparer());
    dict[typeof(object)] = "Hello";

    Console.WriteLine(dict.ContainsKey(typeof(int))); // currently prints false :(

Here's the comparer I tried:

    public class MyComparer : IEqualityComparer<Type>
    {
        public bool Equals(Type x, Type y)
        {
            return y.IsAssignableFrom(x);
        }

        public int GetHashCode(Type obj)
        {
            return obj.GetHashCode();
        }
    }

But it's not working. I'm not quite sure what to return in GetHashCode - I know it's wrong cause when debugging I'm not even reaching Equals - Any idea how to write this correctly? Thanks.

Answer

Marc Gravell picture Marc Gravell · Sep 12, 2014

That simply is not a valid comparer for a dictionary, and the result is not well-defined. Equality comparisons should be commutative, specifically a eq b if and only if b eq a. That does not apply in your case. Likewise, a valid hash-code implementation states that:

  • if two hashes are non-equal, the two values are non-equal
  • two equal values must have the same hash-code

That fails too.

Basically, that isn't going to work.

Specifically, from MSDN:

Notes to Implementers

Implementations are required to ensure that if the Equals method returns true for two objects x and y, then the value returned by the GetHashCode method for x must equal the value returned for y.

The Equals method is reflexive, symmetric, and transitive. That is, it returns true if used to compare an object with itself; true for two objects x and y if it is true for y and x; and true for two objects x and z if it is true for x and y and also true for y and z.