title: Why is my implementation of Equals() not called by AreEqual()
body: <p>I have seen many instructions (<a href="https://stackoverflow.com/questions/7793065/why-does-assert-areequalt-obj1-tobj2-fail-with-identical-objects">Why does Assert.AreEqual(T obj1, Tobj2) fail with identical objects</a>, <a href="https://stackoverflow.com/questions/6328218/unit-test-assert-areequal-failed">Unit Test Assert.AreEqual failed</a>, <a href="https://stackoverflow.com/questions/13825663/assert-areequal-fails-with-the-same-type">Assert.AreEqual fails with the same type</a>) to implement <code>Equals</code>/<code>IEqualityComparer</code> for my classes, if I want to check for equality based on field values rather than reference.</p>
  
  <p>In an attempt to do so, I couldn't get it to work and narrowed the problem down to the following.</p>
  
  <p>My class <em>Subject</em> needs to be tested. In <em>SubjectTestEqual</em>, I would expect both instances to be equal, but the result is "Assert.AreEqual failed. Also, if I debug the code, I see no calls to <em>Equals</em> nor to <em>GetHashCode</em>.</p>
  
  <p>Based on the mystifying "The Default property checks whether type T implements the System.IEquatable interface and, if so, returns an EqualityComparer that uses that implementation. Otherwise, it returns an EqualityComparer that uses the overrides of Object.Equals and Object.GetHashCode provided by T." in the <a href="http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&amp;l=EN-US&amp;k=k%28%22SYSTEM.COLLECTIONS.GENERIC.EQUALITYCOMPARER%601.DEFAULT%22%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK,VERSION=V4.0%22%29;k%28DevLang-CSHARP%29&amp;rd=true" rel="nofollow noreferrer">EqualityComparer.Default Property documentation</a>, I also tried with IEquatable (uncomment code to see the effect) to no avail.</p>
  
  <pre><code>public class Subject : IEqualityComparer&lt;Subject&gt;//, IEquatable&lt;Subject&gt;
  {
      public int Id { get; set; }
  
      public Subject(int name) { Id = name; }
  
      public bool Equals(Subject x, Subject y)
      { return (x.Id == y.Id); }
  
      public int GetHashCode(Subject obj)
      { return Id; }
  
      //public bool Equals(Subject other)
      //{ return Equals(this, other); }
  }
  
  [TestClass]
  public class SubjectTest
  {
      [TestMethod]
      public void SubjectTestEqual()
      { Assert.AreEqual&lt;Subject&gt;(new Subject(1), new Subject(1)); }
  
      [TestMethod]
      public void SubjectTestSame()
      {
          Subject test = new Subject(1);
          Assert.AreEqual&lt;Subject&gt;(test, test);
      }
  }
  </code></pre>
  
  <p>Can anyone shed some light on this?</p>