0

Why do people use GetHashCode and what is it used for. I did a google search on this topic and it is not clear why and when programmers use this method. Can someone explain this GetHashCode method?

Luke101
  • 63,072
  • 85
  • 231
  • 359

3 Answers3

6

The purpose of the method is to create a key for hashtable.

A very good article by Eric Lippert:

http://ericlippert.com/2011/02/28/guidelines-and-rules-for-gethashcode/

What is GetHashCode used for?

It is by design useful for only one thing: putting an object in a hash table. Hence the name.

Why do we have this method on Object in the first place?

It makes perfect sense that every object in the type system should provide a GetType method; data's ability to describe itself is a key feature of the CLR type system. And it makes sense that every object should have a ToString, so that it is able to print out a representation of itself as a string, for debugging purposes. It seems plausible that objects should be able to compare themselves to other objects for equality. But why should it be the case that every object should be able to hash itself for insertion into a hash table? Seems like an odd thing to require every object to be able to do.

I think if we were redesigning the type system from scratch today, hashing might be done differently, perhaps with an IHashable interface. But when the CLR type system was designed there were no generic types and therefore a general-purpose hash table needed to be able to store any object.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • Great article, Neatly explained. – Alam Jun 16 '17 at 15:48
  • Any object can be asked whether it is the same as any other object, even if the other object is of a type it doesn't recognize, if objects can be presumed capable of recognizing the types of any objects they match. The question of whether a particular apple is the same as a particular orange, the fact that one is "comparing apples and oranges" shouldn't pose any difficulty. The answer is simply "no". – supercat Jan 18 '23 at 23:17
4

From MSDN:

A hash code is a numeric value that is used to identify an object during equality testing. It can also serve as an index for an object in a collection

It is faster to use the return value of GetHashCode to determine whether two objects are equal than to call the default implementation of Equals on the object type.

Please see Dictionary(Of TKey, TValue) and also Hashtable Class as they both make use of hash codes to compare elements in the collection. Some LINQ functions such as Enumerable.Distinct make use of hash codes too.

User 12345678
  • 7,714
  • 2
  • 28
  • 46
  • I was going to say, the MSDN documentation is very clear about the purpose and implementation of `GetHashCode`. – Evan L Jun 19 '13 at 17:08
0

Mostly to secure that is required object .. Example if you have object with same value but they not same becouse diffrent position in memory and represent totaly diffrent object (two diffrent instance of same class and same value). When you want to update or delete that object you can do hash to verify that is required object you want to update ... this is in my case

We use this if we work with hibernate of other types and situation

Armin
  • 13
  • 5