简体   繁体   中英

Custom equality comparer for Type in dictionary

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM