Question

I've just noticed that e.g. interface IEqualityComparer<T> provides public int GetHashCode(T obj).
I've seen more often public int GetHashCode() variant in another interfaces/classes. Why it's designed with an input argument? I understand that in 'with argument' case returned value won't be based on caller object, but method's argument. The only way it's needed seems to me to GetHashCode of null object, is it?
And the last question: what should be the caller object(i.e. owner of GetHashCode method)?

Thanks!


UPD. Marked the earliest from right existed answers as the answer. Thanks a lot!

Was it helpful?

Solution

Because IEqualityComparer<T> compares other objects, not itself. Thus it needs two arguments for Equals (compare two other objects) and one argument for GetHashCode (get hash code of other object).

OTHER TIPS

It's the difference between IEqualityComparer and IEquatable.

You see the same differences between IComparer and IComparable.

In both instances, the first type is comparing something else, and so it needs to accept that something else as an argument. The second type of each is comparing "myself" to something, and so there will always be one less parameter (it is in essence the this parameter).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top