Link to home
Start Free TrialLog in
Avatar of RohitYadav
RohitYadav

asked on

GetHashCode

Hi,
Can someone please tell about the GetHashCode method. I have seen it several times and even read about it, but couldn't understand it or where it should be implemented.
Thanks
Avatar of nsanga
nsanga

hash code is something used to compare two objects of same type like

myclass obj1,obj2;

if (obj1=obj2  )
  //do some thing
else
  //do some thing else

here the comparision is between objects and .net uses hashcode to compare the objects. when you don't implement GetHashCode(), every object has a hashcode which is unique for that instance and is only equal to itselt. same thing is done when you use

obj1.equals(obj2)


when you override GetHashCode(), and return some value, .net uses that value for comparision.

Note that when you override GetHashCode(), you need to override Equals() also.






ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RohitYadav

ASKER

Can you please elaborate on the Hashtable? and how can it speed up the search with a very simple example?
Thanks very much Zach
Ok, say you have a class with 10 properties. If the hashtable used a copy of the class, it would have to search every item it contains for those 10 properties. However, that same object will return a number from GetHashCode. So when the Hashtable adds an object to itself it stores the hash code of the object so that it can use that for searching. So instead of doing something like:

if(property1 == itemProperty1 &&
property2 == itemProperty2 &&
property3 == itemProperty3 &&
property4 == itemProperty4 &&
property5 == itemProperty5 &&
property6 == itemProperty6 &&
property7 == itemProperty7 &&
property8 == itemProperty8 &&
property9 == itemProperty9 &&
property10 == itemProperty10)
{
     DoSomething();
}

It does this instead:

if(object.GetHashCode() == itemHashCode)
{
    DoSomething();
}
Avatar of Dmitry G
RohitYadav, hashtable inherently has very high search performsnce so there is no need to speed it up.
The trick is in hashcode. As you already know it's quite unique. Hashtable uses hashcode to calculate some "address" in a hashtable where to put new item to, or, if we are looking for an item, it uses this "address" to look up for the item. Say, if you search an item in a hashtable with thousands of items, it will find the item in 3-4 steps at most. For lists, like ArrayList etc, it may take thousands of steps (if list is not ordered). In big-O notation for hashtables complexity is constant: O(1), and for lists it's O(n) - in other words it depends on the number of items in collection (for hashatables - not).

Usually you do not need to understand hashcode to use hashtables and standard .Net objects. For some custom objects it's needed to provide own algorithms to calculate hashcodes and provide custom equal method. For example, you have a class with int type properties A,B and C. You want count objects of this class equal if all three properties have same values. To speed up the check we may provide custom hashcode algorithm hashciode = A*(B+11)*(C+17) (very oversimplified). So to check if two objects are equal it's enough to compare their hashcodes. Be aware that you algorithm does not provide hashcodes  equal zero, e.g.  (at one company I worked before this bug created lot of problems).
Thanks that helps, i will try to test it myself.