title: Object == Object2 when stored in List<T> but not when object in IEnumerable<T> - WHY?
body: <p><strong>Using ASP MVC5 and EF6.</strong></p>
  
  <p>I had a curious case the other day when I was looking to have different behaviour when a foreach-loop got to the last element.
  The loop wouldn't enter if-condition comparing the object with the result from .Last()-method on the collection.</p>
  
  <p>The collection I was iterating over was something like:</p>
  
  <pre><code>public class CollectionClass{
      IEnumerable&lt;TestClass1&gt; CollectionA
      IEnumerable&lt;TestClass2&gt; CollectionB
  }
  </code></pre>
  
  <p>My code was something like: </p>
  
  <pre><code>DbContext db = new DbContext(); //just for illustration, not actual code
  
  CollectionClass cc = new CollectionClass {
     CollectionA = db.TestClasses1,
     CollectionB = db.TestClasses2
  };
  
  //(TestClasses1 and TestClasses2 are DbSet&lt;T&gt; properties of my DbContext. 
  
  foreach(TestClass1 tc1 in cc.CollectionA)
  {
    if (tc1 == cc.CollectionA.Last()){    //&lt;---NEVER enters in here!!
      //doStuff
    }
    else{
      //doOtherStuff
    }
  }
  </code></pre>
  
  <p>With the code above, the loop never entered into the if-condition, even for the last element, which one would expect.
  But when changed my <em>CollectionClass</em> to:</p>
  
  <pre><code>public class CollectionClass{
      List&lt;TestClass1&gt; CollectionA
      List&lt;TestClass2&gt; CollectionB
  }
  </code></pre>
  
  <p>and instantiated the <em>CollectionClass</em>-object like this:</p>
  
  <pre><code>CollectionClass cc = new CollectionClass {
     CollectionA = db.TestClasses1.ToList(), 
     CollectionB = db.TestClasses2.ToList()
  }; //Added .ToList()
  </code></pre>
  
  <p>the loop entered into the first if-condition at the last iteration as I expected.</p>
  
  <p>Why this difference? Why did the equals-operator (==) evaluate to TRUE when the object had been stored in a List and FALSE when the object was stored in an IEnumerable?
  I know that IEnumerable is an interface -- is that what makes the difference?</p>
  
  <p>I even did an explicit test in the sorts of:</p>
  
  <pre><code>var obj1 = cc.CollectionA.Last();
  var obj2 cc.CollectionA.Last();
  bool result = obj1 == obj2; //result = FALSE
  </code></pre>
  
  <p>and the result was FALSE.</p>