title: How do I implement IEqualityComparer on an immutable generic Pair struct?
body: <p>Currently I have this (edited after reading advice):</p>
  
  <pre><code>struct Pair&lt;T, K&gt; : IEqualityComparer&lt;Pair&lt;T, K&gt;&gt;
  {
      readonly private T _first;
      readonly private K _second;
  
      public Pair(T first, K second)
      {
          _first = first;
          _second = second;
  
      }
  
      public T First { get { return _first; } }
      public K Second { get { return _second; } }
  
      #region IEqualityComparer&lt;Pair&lt;T,K&gt;&gt; Members
  
      public bool Equals(Pair&lt;T, K&gt; x, Pair&lt;T, K&gt; y)
      {
          return x.GetHashCode(x) == y.GetHashCode(y);
      }
  
      public int GetHashCode(Pair&lt;T, K&gt; obj)
      {
          int hashCode = obj.First == null ? 0 : obj._first.GetHashCode();
  
          hashCode ^= obj.Second == null ? 0 : obj._second.GetHashCode();
  
          return hashCode;
      }
  
      #endregion
  
      public override int GetHashCode()
      {
          return this.GetHashCode(this);
      }
  
      public override bool Equals(object obj)
      {
          return (obj != null) &amp;&amp; 
      (obj is Pair&lt;T, K&gt;) &amp;&amp; 
      this.Equals(this, (Pair&lt;T, K&gt;) obj);
      }
  }
  </code></pre>
  
  <p>The problem is that First and Second may not be reference types (VS actually warns me about this), but the code still compiles.  Should I cast them (First and Second) to objects before I compare them, or is there a better way to do this?</p>
  
  <p>Edit:
  Note that I <em>want</em> this struct to support value and reference types (in other words, constraining by class is not a valid solution)</p>
  
  <p>Edit 2:
  As to what I'm trying to achieve, I want this to work in a Dictionary.  Secondly, SRP isn't important to me right now because that isn't really the essence of this problem - it can always be refactored later.  Thirdly, comparing to default(T) will not work in lieu of comparing to null - try it.</p>