Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I read lot of articles regarding GetHashCode usage when it comes to iEqualityComparer . No where it is specified why we require it. Can somebody please help me here.

Below code i tried. My equals method itself is doing the comparison so what is the significance of GetHashCode?

What i know is when we create any object/variable a Hashcode/unique code is going to be get created. For ex when
Class Customer
{
public int Id;
}



Customer c1 = new customer();
Customer c2 = new customer();


c1.value = 10;

c1.value = 10;


Now GetHashCode is diff for both but value is same. Not sure where GetHashCode will help?

In this case

What I have tried:

public class MyClass : System.Collections.Generic.EqualityComparer < MyObject > 
{ 
   public bool Equals(MyObject x, MyObject y) 
   { 
      return x.Equals(y); 
   } 
   public int GetHashCode(MyObject obj) 
   { 
      return obj.GetHashCode(); 
   } 
} 
Posted
Updated 17-May-23 5:47am

 
Share this answer
 
Comments
code4Better 12-Nov-20 7:00am    
Thanks Richard. I read it what i understood is GetHashCode helps in faster retrieval of keys which means faster comparing. Is this true?
Richard Deeming 12-Nov-20 7:25am    
It's primarily used when an object is used as a key in a hash table (eg: a Dictionary<TKey, TValue>). The collection class will use the hash code to divide its contents into several "buckets", which reduces the number of items it has to consider when searching for a particular key.

So long as the hash code is generated properly, it will make the collection operations faster.

It's perfectly possible to have a bad hash code implementation - for example, one that returns a constant value for every object. In that case, the performance of the hash table would degrade.
code4Better 12-Nov-20 8:18am    
Thank you Richard. Appreciate your help!
I agree with the poster. The answer is not correct. This has nothing to do with knowing the properties of the hashes--this has to do with the "contract" that IEqualityComparer implements. Its implementation is wrong! Why? Becuase the Equals function does what the intent of the class is meant to do--compare two objects of the same class. The GetHashCode function takes this operation away from the class and allows some external entity to decide if two hash codes are equal. Hashing is an approximation so x == y is NOT the same as x.hash == y.hash. This is what every computer scientist should understand. The only work around I can see is to force every hash value to be the same i.e. zero to force consumers into call the equals method.

if you don't understand this then consider class B, derived from class A. I want to know if some value of A is in a set { a, b, c } but lets say that b is class B derived from A but has the properties I am trying to find i.e. a.property == find.property... but if i hash b then it is the hash of all values including the properties i do not want to consider, and thus GetHashCode fails to provide the equalitycomparer contract. This is just a dumb implementation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900