Search code examples
c#gethashcode

GetHashCode(obj): why in some cases it's designed with an argument?


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!


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).