title: C# Generic Dictionary TryGetValue doesn't find keys
body: <p>I have this simple example:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  
  namespace ConsoleApplication1
  {
      class Program
      {
          static void Main(string[] args)
          {
              Dictionary&lt;MyKey, string&gt; data = new Dictionary&lt;MyKey, string&gt;();
              data.Add(new MyKey("1", "A"), "value 1A");
              data.Add(new MyKey("2", "A"), "value 2A");
              data.Add(new MyKey("1", "Z"), "value 1Z");
              data.Add(new MyKey("3", "A"), "value 3A");
  
              string myValue;
              if (data.TryGetValue(new MyKey("1", "A"), out myValue))
                  Console.WriteLine("I have found it: {0}", myValue );
  
          }
      }
  
      public struct MyKey
      {
          private string row;
          private string col;
  
          public string Row { get { return row; } set { row = value; } }
          public string Column { get { return col; } set { col = value; } }
  
          public MyKey(string r, string c)
          {
              row = r;
              col = c;
          }
      }
  }
  </code></pre>
  
  <p>This is working fine.  But if I change the MyKey struct by a MyKey class in this way:</p>
  
  <pre><code>public class MyKey
  </code></pre>
  
  <p>Then method <code>TryGetValue</code> doesn't find any key in spite of the key is out there.</p>
  
  <p>I am sure I am missing something obvious but I don't know what.</p>
  
  <p>Any idea ?</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <h2>** Solution **</h2>
  
  <p><strong><em>(please, see accepted solution for better GetHashCode resolution)</em></strong></p>
  
  <p>I have redefined MyKey class like this, and all is working fine now:</p>
  
  <pre><code>public class MyKey
  {
      private string row;
      private string col;
  
      public string Row { get { return row; } set { row = value; } }
      public string Column { get { return col; } set { col = value; } }
  
      public MyKey(string r, string c)
      {
          row = r;
          col = c;
      }
  
      public override bool Equals(object obj)
      {
          if (obj == null || !(obj is MyKey)) return false;
  
          return ((MyKey)obj).Row == this.Row &amp;&amp; ((MyKey)obj).Column == this.Column;
      }
  
      public override int GetHashCode()
      {            
          return (this.Row + this.Column).GetHashCode();
      }    
  }
  </code></pre>
  
  <p>Thanks to all people answered this.</p>