title: collections.Contains(T) method
body: <p>I am using a <code>System.Collections.Generic</code>, which contains instances of a class I wrote.</p>
  
  <p>I have read that the collections <code>.Contains</code> method uses <code>object.Equals()</code>, or an implementation of the <code>Equals()</code> method from the <code>IEquatable</code> interface.</p>
  
  <p>I have overridden the object method, as well as implemented from the interface. However, <code>Queue.Contains(instance)</code> is always returning false. What am I doing wrong?</p>
  
  <p>For instance...</p>
  
  <pre><code>class Foo : IEquatable&lt;Foo&gt;
  {
      ...
      int fooField1;
      ...
  
      public override bool Equals(object obj)
      {
           Foo other = obj as Foo;
           bool isEqual = false;
  
           if (other.fooField1 == this.fooField1)
           { 
               isEqual = true;
           }
  
           return isEqual;         
      }
  
      public bool Equals(Foo other)
      {
           bool isEqual = false;
  
           if (other.fooField1 == this.fooField1)
           { 
               isEqual = true;
           }
  
           return isEqual;         
      }
  
  }
  ...
  
  void SomeFunction()
  {
      Queue&lt;Foo&gt; Q = new Queue&lt;Foo&gt;();
      Foo fooInstance1 = new Foo();
      Foo fooInstance2 = new Foo();
  
      fooInstance1.fooField1 = 5;
      fooInstance2.fooField1 = 5;
  
      Q.Enqueue(fooInstanec1);
      if(Q.Contains(fooInstance2) == false)
      {
           Q.Enqueue(fooInstance2);
      }
  }
  </code></pre>
  
  <p>fooInstance2 is always added to the queue. In fact, when I run this on the debugger, the implementations of Equals are never reached.</p>
  
  <p>What am I doing wrong?</p>