Listing 1. A simple C# enumerator

class ArrayEnumerator : IEnumerator<int>

{
    int idx = 0;
    int[] coll;

    public ArrayEnumerator(int[] items)
    {
        this.coll = coll;
        this.idx = -1;
    }

    public int Current
    {
        get { return coll[idx]; }
    }


    public bool MoveNext()
    {
        if (idx < coll.GetLength(0) - 1)
        {
            idx++; return true;
        }

        return false;
    }

    public void Dispose() { }
}

class NumberCollection : IEnumerable<int>
{
    int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    public IEnumerator<int> GetEnumerator()
    {
        return new ArrayEnumerator(nums);
    }
}

Listing 2

>>>e=hello()
>>>e.next()
hello
>>>e.next()
world


Listing 3. Trying to emulate Python generators

class Hello : IEnumerable<string>

{
    class HelloEnumerator : IEnumerator<string>
    {
        int State = 0;
        string _Current = null;

        public string Current
        {
            get
            {
                return _Current;
            }
        }


        public bool MoveNext()
        {
            switch (State)
            {
                case 0:
                    _Current = "Hello";
                    goto end;

                case 1:
                    _Current = "World";
                    goto end;

                default:
                    return false;
            }

        end:
		State++;
		return true;
        }

        public void Dispose()
        {
        }
    }

    public IEnumerator<string> GetEnumerator()
    {
        Console.WriteLine("Hello.GetEnumerator");
        return new HelloEnumerator();
    }
}

Listing 4: Solving the Tower of Hanoi with a Generator

class Greeter
{
   static IEnumerable<string> SayHello()
   {
      yield return "hello";
      yield return "world";
   }
}

Listing 5

private sealed class <GetStrings>d__0 :IEnumerable<string>,
                                       IEnumerator<string>
{
      private int <>1__state;
      private string <>2__current;
      public <GetStrings>d__0(int <>1__state);

      private bool MoveNext();
      void Reset();
      string Current { get; }

      //...other stuff cut for clarity...
}


Listing 6. Compiler-generated MoveNext function

private bool Greeter.MoveNext()
{
      switch (this.<>1__state)
      {
            case 0:
            {
                  goto Label_001F;
            }
            case 1:
            {
                  goto Label_001B;
            }
            case 2:
            {
                  goto Label_001D;
            }
      }
      goto Label_0021;
Label_001B:
      goto Label_0040;
Label_001D:
      goto Label_005D;
Label_001F:
      goto Label_0023;
Label_0021:
      goto Label_0064;
Label_0023:
      this.<>1__state = -1;
      this.<>2__current = "hello";
      this.<>1__state = 1;
      return true;
Label_0040:
      this.<>1__state = -1;
      this.<>2__current = "world";
      this.<>1__state = 2;
      return true;
Label_005D:
      this.<>1__state = -1;
Label_0064:
      return false;
}


Listing 7

  if   n=1   then     move one ring from peg  A   to peg B
      else     move   n-1   rings from peg   A   to peg   C
                  move 1 ring from peg   A   to peg   B
                  move   n-1   rings from peg   C  to peg  B

Listing 8. Solving the Tower of Hanoi with a Generator

using System.Collections.Generic;

#endregion

public enum Peg { A, B, C };

public struct Move
{
    public Move(Peg from, Peg to) { From = from; To = to; }
    public readonly Peg From;
    public readonly Peg To;
}

class TowerofHanoi
{

    public static IEnumerable<Move> GenerateMoves(int n,
                                                  Peg from,
                                                  Peg to,
                                                  Peg scratch)
    {
        if (n == 1)
            yield return new Move(from, to);
        else
        {
            foreach (Move m in GenerateMoves(n - 1, from, scratch, to))
                yield return m;

            yield return new Move(from, to);

            foreach (Move m in GenerateMoves(n - 1, scratch, to, from))
                yield return m;
        }
    }

}
class Program
{
	static void Main(string[] args)
	{
        foreach (Move m in TowerofHanoi.GenerateMoves(10, Peg.A,
                                                      Peg.B, Peg.C))
        {
            System.Console.WriteLine("From {0} to {1}", m.From, m.To);

        }

    }
}