title: IEnumerable vs List - What to Use? How do they work?
body: <p>I have some doubts over how Enumerators work, and LINQ. Consider these two simple selects:</p>
  
  <pre><code>List&lt;Animal&gt; sel = (from animal in Animals 
                      join race in Species
                      on animal.SpeciesKey equals race.SpeciesKey
                      select animal).Distinct().ToList();
  </code></pre>
  
  <p>or</p>
  
  <pre><code>IEnumerable&lt;Animal&gt; sel = (from animal in Animals 
                             join race in Species
                             on animal.SpeciesKey equals race.SpeciesKey
                             select animal).Distinct();
  </code></pre>
  
  <p>I changed the names of my original objects so that this looks like a more generic example. The query itself is not that important. What I want to ask is this:</p>
  
  <pre><code>foreach (Animal animal in sel) { /*do stuff*/ }
  </code></pre>
  
  <ol>
  <li><p>I noticed that if I use <code>IEnumerable</code>, when I debug and inspect "sel", which in that case is the IEnumerable, it has some interesting members: "inner", "outer", "innerKeySelector" and "outerKeySelector", these last 2 appear to be delegates. The "inner" member does not have "Animal" instances in it, but rather "Species" instances, which was very strange for me. The "outer" member does contain "Animal" instances. I presume that the two delegates determine which goes in and what goes out of it?</p></li>
  <li><p>I noticed that if I use "Distinct", the "inner" contains 6 items (this is incorrect as only 2 are Distinct), but the "outer" does contain the correct values. Again, probably the delegated methods determine this but this is a bit more than I know about IEnumerable.</p></li>
  <li><p>Most importantly, which of the two options is the best performance-wise?</p></li>
  </ol>
  
  <p>The evil List conversion via <code>.ToList()</code>?</p>
  
  <p>Or maybe using the enumerator directly?</p>
  
  <p>If you can, please also explain a bit or throw some links that explain this use of IEnumerable.</p>