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:
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.