I know that when I create a dictionary with a custom class as key, the match when I provide a key is done through reference compare. For example:
public class SomeClass
{
public object SomeValue { get; set; }
}
// ....
public static void Main()
{
var dict = new Dictionary<SomeClass, string>();
var key1 = new SomeClass { SomeValue = 30 };
dict[key1] = "30";
Console.WriteLine(dict[key1]); // prints "30"
var key2 = new SomeClass { SomeValue = 30 };
Console.WriteLine(dict[key2]); // prints null
}
What happens if I override Equals (and ==) in the SomeClass class? Will I get “30” on the second line of the output?
And what if I want to have a dictionary that is based on references instead of member values, but I have overridden Equals?
Thanks!!
Short Answer
Yes if you override
EqualsandGetHashCodemethods your custom key comparison will start working.Long Answer
The
Dictionary<TKey,TValue>class does not necessarily do a reference based comparison. It instead uses theIEqualityComparer<TKey>instance which can be provided to the constructor. If not provided the default isEqualityComparer<T>.Default.The processes by which
EqualityComparer<T>.Defaultworks is complicated. But a summary isIEquatable<T>on the type and if present it is used for equalityEqualsmethod which by default isObject.Equalsand hence a reference comparisonSo types can override the the comparison at a couple of levels
IEqualityComparer<T>IEquatable<T>and overridingGetHashCodeEqualsandGetHashCodeThe equality operators
==and!=do not come into play for theTKeytype in aDictionary<TKey,TValue>.