Tony said:
Peter Duniho said:
System.Object.GetHashCode() and System.Object.Equals(), which may or may
not be overridden in the class used as TKey. [...]
At the end is some text from the e-learning that I use.
I just wonder what this sentence mean ?
"If you do not specify an implementation, the default generic equality
comparer EqualityComparer.Default is used. The EqualityComparer.Default
is the public read-only version of EqualityComparer and returns the
default generic argument. It also provides a base class for
implementations of the IEqualityComparer generic interface."
I mean if I don't specify an implementation meaning passing no argument
to the Dictionary c-tor
then your answer(row below) should be valid and not what the above
sentence say.
They say the same thing. Take a look at the implementation of the default
EqualityComparer. Or better yet, just check the documentation. From
http://msdn.microsoft.com/en-us/library/ms224763.aspx:
The Default property checks whether type T implements the
System.IEquatable<T> generic interface and if so returns an
EqualityComparer<T> that uses that implementation. Otherwise
it returns an EqualityComparer<T> that uses the overrides of
Object.Equals and Object.GetHashCode provided by T.
the GetHashCode() and said:
To determine whether keys are equal, the generic Dictionary class needs
an equality implementation of the generic IEqualityComparer interface.
But YOU do not need to provide one. It is NOT important to ALWAYS supply
one. As long as you are satisfied with the existing implementation of
IEquatable<T> or the overrides of GetHashCode() and Equals() in the class
you are using for TKey, there's no need to provide an explicit comparer.
The default one will do just what you want.
Pete