title: Why isn't IEnumerable consumed?/how do generators work in c# compared to python
body: <p>So I thought I understood c# yield return as being largely the same as pythons yield which I thought that I understood. I thought that the compiler transforms a function into an object with a pointer to where execution should be resumed and when a request for the next value comes along the object runs up to the next yield where it updates the pointer of where to resume execution and returns a value. </p>
  
  <p>In python this works sort of similarly to lazy evaluation in that it produces values as needed but once the values are used once they can be gc'ed if not save in another variable. Trying to iterate over the result of such a function twice returns an empty iterable unless you transform it to a list. </p>
  
  <p>ex.</p>
  
  <pre><code>def y():
      list = [1,2,3,4]
  
      for i in list:
          yield str(i)
  
  ys = y()
  print "first ys:"
  print ",".join(ys)
  print "second ys:"
  print ",".join(ys)
  </code></pre>
  
  <p>outputs</p>
  
  <pre><code>first ys:
  1,2,3,4
  second ys:
  </code></pre>
  
  <p>Until recently I thought the same thing was true for c# but trying it out in dotnetfiddle failed. </p>
  
  <p><strong><a href="http://dotnetfiddle.net/W5Cbv6" rel="noreferrer">http://dotnetfiddle.net/W5Cbv6</a></strong></p>
  
  <pre><code>using System;
  using System.Linq;
  using System.Collections.Generic;
  
  public class Program
  {
      public static IEnumerable&lt;string&gt; Y()
      {
          var list = new List&lt;string&gt; {"1","2","3","4","5"};
          foreach(var i in list)
          {
              yield return i;
          }
      }
  
      public static void Main()
      {
  
  
          var ys = Y();
          Console.WriteLine("first ys");
          Console.WriteLine(string.Join(",", ys));
          Console.WriteLine("second ys");
          Console.WriteLine(string.Join(",", ys));
  
      }
  }
  </code></pre>
  
  <p>outputs </p>
  
  <pre><code>first ys
  1,2,3,4,5
  second ys
  1,2,3,4,5
  </code></pre>
  
  <p>What is happening here? Is it caching the result? It can't be right, otherwise <a href="http://msdn.microsoft.com/en-us/library/dd383503%28v=vs.110%29.aspx" rel="noreferrer">File.ReadLines</a> would blow up on huge files? Is it simply restarting the function from the top a second time?</p>
  
  <p>note: I'm a bit uncertain about some of the terminology of generators and coroutines so I've tried to avoid labelling. </p>