title: How do I implement IEnumerable<T>
body: <p>I know how to implement the non generic IEnumerable, like this:</p>
  
  <pre><code>using System;
  using System.Collections;
  
  namespace ConsoleApplication33
  {
      class Program
      {
          static void Main(string[] args)
          {
              MyObjects myObjects = new MyObjects();
              myObjects[0] = new MyObject() { Foo = "Hello", Bar = 1 };
              myObjects[1] = new MyObject() { Foo = "World", Bar = 2 };
  
              foreach (MyObject x in myObjects)
              {
                  Console.WriteLine(x.Foo);
                  Console.WriteLine(x.Bar);
              }
  
              Console.ReadLine();
          }
      }
  
      class MyObject
      {
          public string Foo { get; set; }
          public int Bar { get; set; }
      }
  
      class MyObjects : IEnumerable
      {
          ArrayList mylist = new ArrayList();
  
          public MyObject this[int index]
          {
              get { return (MyObject)mylist[index]; }
              set { mylist.Insert(index, value); }
          }
  
          IEnumerator IEnumerable.GetEnumerator()
          {
              return mylist.GetEnumerator();
          }
      }
  }
  </code></pre>
  
  <p>However I also notice that IEnumerable has a generic version, <code>IEnumerable&lt;T&gt;</code>, but I can't figure out how to implement it.</p>
  
  <p>If I add <code>using System.Collections.Generic;</code> to my using directives, and then change:</p>
  
  <pre><code>class MyObjects : IEnumerable
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>class MyObjects : IEnumerable&lt;MyObject&gt;
  </code></pre>
  
  <p>And then right click on <code>IEnumerable&lt;MyObject&gt;</code> and select <code>Implement Interface =&gt; Implement Interface</code>, Visual Studio helpfully adds the following block of code:</p>
  
  <pre><code>IEnumerator&lt;MyObject&gt; IEnumerable&lt;MyObject&gt;.GetEnumerator()
  {
      throw new NotImplementedException();
  }
  </code></pre>
  
  <p>Returning the non generic IEnumerable object from the <code>GetEnumerator();</code> method doesn't work this time, so what do I put here? The CLI now ignores the non generic implementation and heads straight for the generic version when it tries to enumerate through my array during the foreach loop.</p>