Dictionary<>
will take adventage of key's equality comparer when call Add
, Remove
, Contains
and other methods. If we don't pass a comparer when declaring dictionary, it will use the default comparer. The default comparer will use key's Equals
method to compare. So we have two solutions:
- Provide a
MyKeyEqualityComparer
and override itsEquals(TKey x, TKey y)
method. - Use
EqualityComparer.Default
and overrideMyKey
'sEquals
method.