Hash table and GetHashCode()

Posted on 2005-05-03
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;
Question by:__alex
    LVL 8

    Accepted Solution

    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.


    LVL 37

    Assisted Solution

    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.



    LVL 8

    Expert Comment

    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.


    LVL 37

    Expert Comment

    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.

    LVL 2

    Author Comment

    Very interesting, indeed.
    Thanks a lot!

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now