title: How .NET Dictionary implementation works with mutable objects
body: <p>I understand that it is not advisable to use "mutable" objects (objects whose GetHashCode() method can return different results while they being used as keys in a <a href="http://msdn.microsoft.com/en-us/library/xfhwa508.aspx" rel="nofollow">Dictionary</a>). </p>
  
  <p>Below is my understanding of how a dictionary, which is implemented as a hash table, works:</p>
  
  <p>When I am adding new key, for example <code>dict.Add(m1, "initially here was m1 object");</code>, <code>dict</code> calculates the hashcode of <code>m1</code> using the <code>GetHashCode()</code> method. Then it does some internal calculations and finally puts this object into some position of its internal array.</p>
  
  <p>When I am using the key index to get the value, for example <code>dict[m1]</code>, <code>dict</code> calculates the hashcode again. Then it does some internal calculations, and it gives me an object which is located at the calculated position inside of its internal array.</p>
  
  <p>But I think there is an error which I can't find. </p>
  
  <p>So lets assume that I have this code:</p>
  
  <pre><code>    class MutableObject
      {
           Int32 m_value;
  
           public MutableObject(Int32 value)
           {
               m_value = value;
           }
  
           public void Mutate(Int32 value)
           {
               m_value = value;
           }
  
           public override int GetHashCode()
           {
               return m_value;
           }
     }
  
     static void Main(string[] args)
     {
           MutableObject m1 = new MutableObject(1);
           MutableObject m2 = new MutableObject(2);
  
           var dict = new Dictionary&lt;MutableObject, String&gt;();
           dict.Add(m1, "initially here was m1 object");
           dict.Add(m2, "initially here was m2 object");
  
           Console.WriteLine("Before mutation:");
           Console.WriteLine("dict[m1] = " + dict[m1]);
           Console.WriteLine("dict[m2] = " + dict[m2]);
  
           m1.Mutate(2);
           m2.Mutate(1);
  
           Console.WriteLine("After mutation:");
           Console.WriteLine("dict[m1] = " + dict[m1]);
           Console.WriteLine("dict[m2] = " + dict[m2]);
  
           Console.ReadKey(true);
     }
  </code></pre>
  
  <p>When I call <code>Mutate</code> methods, keys are swapped. So I thought it will give swapped results. But actually this line: <code>Console.WriteLine("dict[m1] = " + dict[m1]);</code> throws KeyNotFoundException, and I can't understand why. Obviously I am missing something here...</p>