I have implemented a Dictionary as follows:
Dictionary<ErrorHashKey, ErrorRow> dictionary;
I have defined Equals()
and GetHashCode()
in the ErrorHashKey
class. I am currently writing up some documentation for the project, and came accross this from the IEqualityComparer Interface doc:
Dictionary requires an equality implementation to determine whether keys are equal. You can specify an implementation of the IEqualityComparer generic interface by using a constructor that accepts a comparer parameter; if you do not specify an implementation, the default generic equality comparer EqualityComparer.Default is used. If type TKey implements the System.IEquatable generic interface, the default equality comparer uses that implementation.
I am not doing anything that the documentation specifies (or at least I don't think I am). I do not pass a comparer in the constructor parameter nor do I create an EqualityComparer.Default
comparer.
Is the System.IEquatable<T> generic interface
automatically implemented in every class created? Should I be defining an implementation of IEqualityComparer<T>
?
The default comparer will call object.Equals
or object.GetHashCode
(your overridden methods) if IEquatable<T>
is not implemented. This is documented at the documentation for EqualityComparer<T>.Default
. You don't need to do anything extra, and no, IEquatable<T>
is not automatically implemented in your class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With