title: C# dictionary value reference type - please explain why this happens
body: <p>I don't understand the results of the following linqpad query in C#. The comments should explain where I am confused.</p>
  
  <pre><code>void Main()
  {
      Dictionary&lt;string, testClass&gt; test = new Dictionary&lt;string, testClass&gt;();
  
      string key = "key";
      testClass val = null;
  
      test.Add(key, val);
  
      val = new testClass();
  
      test[key].Dump(); //returns null.   WHAT? I just set it!!!
  
  
  
      test[key] = val;
      val.Text = "something";
      //  returns val object, with Text set to "Something". 
      //  If the above didn't work, why does this work?
      test[key].Dump(); 
  
  
  
      val.Text = "Nothing";
      //  return val object, with Text set to "Nothing". 
      //  This, I expect, but, again, why didn't the first example work?
      test[key].Dump(); 
  
  
  
      val = null;
      //  returns val object, with Text set to "Nothing"...WHAT?? 
      //  Now my head is going to explode...
      test[key].Dump(); 
  
  }
  
  // Define other methods and classes here
  
  public class testClass
  {
      public override string ToString()
      {
          return Text;
      }
  
      public string Text { get; set;}     
  }
  </code></pre>