What is the HashCode

G

Guest

Hi to all,

In a lot of type of classes in the .net framework classes library i see that
has the method GethashCode.

What is the HashCode of anything, and in wich kind of functionality are used
for?
 
L

Lebesgue

Hash code is a unique value identifying every object.
It is a virtual method, so you may override it and provide different
implementation if you need different behavior.
It is used for adding objects into collections called Hashtables.
One can add object-key pair to hashtable and later, retrieve the object by
specifying the key.
Example:
Hashtable table = new Hashtable();
MemoryStream ms = new MemoryStream();
ms.Write(someBytes);
table.Add("MyStream", ms);

and later, if you want your stream back
MemoryStream ms = (MemoryStream)table["MyStream"];

Hashtable operations are constant-time operations, so adding and removing
elements is very fast.
Hope this explains enough.
 
B

Brian Gideon

Chad,

Careful, "sorted" probably isn't the best term for what you're trying
to describe. In a hashtable the items are anything but sorted.

Brian
 
L

Lebesgue

Oh, I forgot to mention, the key's GetHashCode() method is called
automatically when you insert key-value pair to the hashtable
So table.Add("MyStream", obj);
is the same as table.Add("MyStream".GetHashCode(), obj);

Lebesgue said:
Hash code is a unique value identifying every object.
It is a virtual method, so you may override it and provide different
implementation if you need different behavior.
It is used for adding objects into collections called Hashtables.
One can add object-key pair to hashtable and later, retrieve the object by
specifying the key.
Example:
Hashtable table = new Hashtable();
MemoryStream ms = new MemoryStream();
ms.Write(someBytes);
table.Add("MyStream", ms);

and later, if you want your stream back
MemoryStream ms = (MemoryStream)table["MyStream"];

Hashtable operations are constant-time operations, so adding and removing
elements is very fast.
Hope this explains enough.
 
B

Brian Gideon

Lebesque,

Actually, hash codes are not guarenteed to uniquely identify objects.
What can be said about them is that if two objects are equal then their
hash codes must be equal. However, the converse is not true. In other
words, two objects with the same hash code do not necessarily have to
be equal.

Brian
 
B

Brian Gideon

Lebesgue,

table.Add("MyStream", obj) and table.Add("MyStream".GetHashCode(), obj)
are *not* equivalent.

In the first example a string is the key and in the second example an
integer is the key. A string and an integer certainly are not equal
because at the very least they differ in type.

The GetHashCode method is called internally by the Hashtable so that it
can figure out where the item is stored. The technique that the .NET
hashtable uses to accomplish this is called quadratic probing.

Brian
 
L

Lebesgue

Brian,
you are right.
I just wanted to explain the principle to someone who doesn't know how it
all works.
I am sorry for possible confusion.
 
B

Brian Gideon

Lebesgue,

Don't be sorry. The purpose of this forum is to learn. Sometimes you
end up learning by attempting to answer someone else's question. I
think a large percentage of us on here, including me, have been guilty
of misinformation at some point.

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top