Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Hash table and GetHashCode()

Posted on 2005-05-03
5
Medium Priority
?
260 Views
Last Modified: 2010-04-16
What happens if I change the hash code after adding my object to a hashtable? I suppose my object is now in the wrong bucket?
An example:

class MyClass {
    public int i;
    public MyClass(int i) {
        this.i = i;
    }
    public override GetHashCode() {
        return i;
    }
    static void Main() {
        ht = new HashTable();
        mc = new MyClass(1);
        ht.Add(mc, 1);
        mc.i = 2;
    }
}
0
Comment
Question by:__alex
  • 2
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
Razzie_ earned 600 total points
ID: 13917612
When you add a key to the hashtable, its hash is calculated. If you request a value using a key, that hash is calculated and compared against the keys in the Hashtable.

So in your example, if you changed the instance of myClass you will get a different hash - and you won't be able to find the value in the hashtable using your instance of myClass as the key.

HTH,

Razzie
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 600 total points
ID: 13918517
as razzie states it will just consider them to be different in the short term...

Although what is intresting with this is that it may end up correcting itself (especially if you add more objects)...

occasionally when a hash table's fill level gets too high it will grow the internal data structure ... when this happens it will recompute the hash of all interior objects and reinsert them into the new table. Since it is a reference type, you have changed the same object that the hash table is using (as opposed to a value type where it would have its own copy of the object) thus when it goes to recompute it will retrieve the new hash value and move your object accordingly.

relying on this mechanism would be a VERY poor practice and should be considerred non-deterministic at best.

Cheers,

Greg

0
 
LVL 8

Expert Comment

by:Razzie_
ID: 13920230
Cool Greg, I was suspecting it might do something like that, but didn't know it for sure. So that must mean that a Hashtable stores the hash, a value, and the object itself somewhere internally? Or it wouldn't be able to recalculate the new hash.

Cheers,

Razzie
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 13920663
most hash implementations deal with an internal array ...

what you do is hashvalue % size of array to find the right bucket to start then compare ...

it only maintains a reference to the object (it needs to in order to be able to return it later when you ask for it) it uses this same reference to generate its hashes ...

whether it recalls the gethashcode method or uses a cached version from a previous call is an implementational detail ... as I said at best non-deterministic.

Greg
0
 
LVL 2

Author Comment

by:__alex
ID: 13924656
Very interesting, indeed.
Thanks a lot!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question