• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

Hash table and GetHashCode()

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
__alex
Asked:
__alex
  • 2
  • 2
2 Solutions
 
Razzie_Commented:
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
 
gregoryyoungCommented:
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
 
Razzie_Commented:
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
 
gregoryyoungCommented:
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
 
__alexAuthor Commented:
Very interesting, indeed.
Thanks a lot!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

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

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now