title: Can IEquatable compare custom objects having a list property of other custom objects?
body: <p>I'd like to compare two custom class objects of the same type. The custom class being compared has a List property which is filled with items of another custom type. Is this possible by inheriting IEquatable? </p>
  
  <p>I couldn't figure out how to make this work by modifying <a href="http://msdn.microsoft.com/en-us/library/ms131190%28v=vs.110%29.aspx" rel="nofollow">MSDN's code</a> to compare class objects containing List properties of a custom type.</p>
  
  <p>I did successfully derive from the <a href="http://msdn.microsoft.com/en-us/library/ms132151%28v=vs.110%29.aspx" rel="nofollow">EqualityComparer class</a> to make a separate comparison class (code below), but I'd like to implement the comparison ability in the actual classes being compared. Here's what I have so far:</p>
  
  <p><strong>EDIT: This doesn't work after all. My apologies - I've been working on this awhile and I may have pasted incorrect example code. I'm working on trying to find my working solution...</strong></p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
       // Test the ContractComparer.
  
          Contract a = new Contract("Contract X", new List&lt;Commission&gt;() { new Commission(1), new Commission(2), new Commission(3) });
          Contract b = new Contract("Contract X", new List&lt;Commission&gt;() { new Commission(1), new Commission(2), new Commission(3) });
  
          ContractComparer comparer = new ContractComparer();
  
          Console.WriteLine(comparer.Equals(a, b));
      // Output returns True. I can't get this to return
          // True when I inherit IEquatable in my custom classes
          // if I include the list property ("Commissions") in my
          // comparison.
  
          Console.ReadLine();
      }
  }
  
  public class Contract
  {
      public string Name { get; set; }
      public List&lt;Commission&gt; Commissions { get; set; }
      public Contract(string name, List&lt;Commission&gt; commissions)
      {
          this.Name = name;
          this.Commissions = commissions;
      }
  }
  
  public class Commission
  {
      public int ID;
      public Commission(int id)
      {
          this.ID = id;
      }
  }
  
  public class ContractComparer : IEqualityComparer&lt;Contract&gt;
  {
  
      public bool Equals(Contract a, Contract b)
      {
          //Check whether the objects are the same object.  
          if (Object.ReferenceEquals(a, b)) return true;
  
          //Check whether the contracts' properties are equal.  
          return a != null &amp;&amp; b != null &amp;&amp; a.Name.Equals(b.Name) &amp;&amp; a.Commissions.Equals(b.Commissions);
      }
  
      public int GetHashCode(Contract obj)
      {
          int hashName = obj.Name.GetHashCode();
          int hashCommissions = obj.Commissions.GetHashCode();
  
          return hashName ^ hashCommissions;
      }
  }
  </code></pre>