title: C#: How to translate the Yield Keyword
body: <ol>
  <li>What would the MSDN sample look like without the <strong>yield</strong> keyword? You may use any example if you perfer. I would just like to understand what is going on under the hood.</li>
  <li>Is the <strong>yield</strong> operator <strong>eagerly</strong> or <strong>lazily</strong> evaluated?</li>
  </ol>
  
  <p><strong>Sample:</strong></p>
  
  <pre><code>using System;
  using System.Collections;
  public class List
  {
      public static IEnumerable Power(int number, int exponent)
      {
          int counter = 0;
          int result = 1;
          while (counter++ &lt; exponent)
          {
              result = result * number;
              yield return result;
          }
      }
  
      static void Main()
      {
          // Display powers of 2 up to the exponent 8:
          foreach (int i in Power(2, 8))
          {
              Console.Write("{0} ", i);
          }
      }
  }
  </code></pre>
  
  <ul>
  <li><a href="http://msdn.microsoft.com/en/library/9k7k7cf0%28v=vs.80%29.aspx" rel="nofollow">MSDN - Yield Keyword</a></li>
  </ul>
  
  <hr>
  
  <p>If the yield operator is eagerly evaluated here is my guess:</p>
  
  <pre><code>    public static IEnumerable Power(int number, int exponent)
      {
          int counter = 0;
          int result = 1;
          List&lt;int&gt; powers;
          while (counter++ &lt; exponent)
          {
              result = result * number;
              powers.add(result);
          }
          return powers;
      }
  </code></pre>
  
  <p>I have no clue what it might look like if the yield operator is lazily evaluated.</p>
  
  <p><strong>Update:</strong> Reflector gives this:</p>
  
  <pre><code>public class List
  {
      // Methods
      public List();
      private static void Main();
      public static IEnumerable Power(int number, int exponent);
  
      // Nested Types
      [CompilerGenerated]
      private sealed class &lt;Power&gt;d__0 : IEnumerable&lt;object&gt;, IEnumerable, IEnumerator&lt;object&gt;, IEnumerator, IDisposable
      {
          // Fields
          private int &lt;&gt;1__state;
          private object &lt;&gt;2__current;
          public int &lt;&gt;3__exponent;
          public int &lt;&gt;3__number;
          private int &lt;&gt;l__initialThreadId;
          public int &lt;counter&gt;5__1;
          public int &lt;result&gt;5__2;
          public int exponent;
          public int number;
  
          // Methods
          [DebuggerHidden]
          public &lt;Power&gt;d__0(int &lt;&gt;1__state);
          private bool MoveNext();
          [DebuggerHidden]
          IEnumerator&lt;object&gt; IEnumerable&lt;object&gt;.GetEnumerator();
          [DebuggerHidden]
          IEnumerator IEnumerable.GetEnumerator();
          [DebuggerHidden]
          void IEnumerator.Reset();
          void IDisposable.Dispose();
  
          // Properties
          object IEnumerator&lt;object&gt;.Current { [DebuggerHidden] get; }
          object IEnumerator.Current { [DebuggerHidden] get; }
      }
  }
  
  IEnumerator&lt;object&gt; IEnumerable&lt;object&gt;.GetEnumerator()
  {
      List.&lt;Power&gt;d__0 d__;
      if ((Thread.CurrentThread.ManagedThreadId == this.&lt;&gt;l__initialThreadId) &amp;&amp; (this.&lt;&gt;1__state == -2))
      {
          this.&lt;&gt;1__state = 0;
          d__ = this;
      }
      else
      {
          d__ = new List.&lt;Power&gt;d__0(0);
      }
      d__.number = this.&lt;&gt;3__number;
      d__.exponent = this.&lt;&gt;3__exponent;
      return d__;
  }
  
  
  
  private bool MoveNext()
  {
      switch (this.&lt;&gt;1__state)
      {
          case 0:
              this.&lt;&gt;1__state = -1;
              this.&lt;counter&gt;5__1 = 0;
              this.&lt;result&gt;5__2 = 1;
              while (this.&lt;counter&gt;5__1++ &lt; this.exponent)
              {
                  this.&lt;result&gt;5__2 *= this.number;
                  this.&lt;&gt;2__current = this.&lt;result&gt;5__2;
                  this.&lt;&gt;1__state = 1;
                  return true;
              Label_0065:
                  this.&lt;&gt;1__state = -1;
              }
              break;
  
          case 1:
              goto Label_0065;
      }
      return false;
  }
  </code></pre>