title: Have I implemented Equals()/GetHashCode() correctly?
body: <p>The program was working with this implementation:</p>
  
  <pre><code>class Instrument
  {
      public string ClassCode { get; set; }
      public string Ticker { get; set; }
      public override string ToString()
      {
          return " ClassCode: " + ClassCode + " Ticker: " + Ticker + '.';
      }
  }
  </code></pre>
  
  <p>But because I need to use Instrument in Dictionary I've decided to implement equals/hashcode:</p>
  
  <pre><code>class Instrument
  {
      public string ClassCode { get; set; }
      public string Ticker { get; set; }
      public override string ToString()
      {
          return " ClassCode: " + ClassCode + " Ticker: " + Ticker + '.';
      }
  
      public override bool Equals(object obj)
      {
          if (obj == null)
              return false;
  
          Instrument instrument = obj as Instrument;
          if (instrument == null)
              return false;
  
          return ((ClassCode.Equals(instrument.ClassCode)) &amp;&amp; (Ticker.Equals(instrument.Ticker));
      }
  
      public override int GetHashCode()
      {
          int hash = 13;
          hash = (hash * 7) + ClassCode.GetHashCode();
          hash = (hash * 7) + Ticker.GetHashCode();
          return hash;
      }
  }
  </code></pre>
  
  <p>Now the program has stopped working. In such or similar places I receive "KeyNotFoundException":</p>
  
  <pre><code>if (cache.Keys.Any(instrument =&gt; instrument.Ticker == newTicker &amp;&amp; instrument.ClassCode == newClassCode))
  </code></pre>
  
  <p>Is it possible that some pieces of the code assume that equals and hashcode IS NOT implemented?
  Or probably I just implemented them wrong? Sorry I'm not familiar with such advanced features in C# as the last piece of code and don't know how it is connected with equals or hashCode.</p>