title: Implementing IEnumerable<T> in C++/CLI
body: <p>I'm having problems implementing <code>IEnumerable&lt;T&gt;</code> in my custom collection class in C++/CLI. Here is the relevant part of the code:</p>
  
  <pre><code>using namespace System::Collections::Generic;
  
  ref class MyCollection : IEnumerable&lt;MyClass^&gt;
  {
  public:
      MyCollection()
      {
      }  
  
      virtual IEnumerator&lt;MyClass^&gt;^ GetEnumerator()
      {
          return nullptr;
      }
  };
  </code></pre>
  
  <p>When compiled, this results in the following errors:</p>
  
  <blockquote>
    <p>error C2392:
    'System::Collections::Generic::IEnumerator
    ^MyCollection::GetEnumerator(void)':
    covariant returns types are not
    supported in managed types, otherwise
    'System::Collections::IEnumerator
    ^System::Collections::IEnumerable::GetEnumerator(void)'
    would be overridden error C3766:
    'MyCollection' must provide an
    implementation for the interface
    method
    'System::Collections::IEnumerator
    ^System::Collections::IEnumerable::GetEnumerator(void)'</p>
  </blockquote>
  
  <p>This makes sense, since <code>IEnumerable&lt;T&gt;</code> derives from <code>IEnumerable</code>. However, I'm not sure how to fix this compile error. If this was C#, I would implicitly implement <code>IEnumerable</code>, however I'm not sure how to do that in C++/CLI (if that's even possible) like this:</p>
  
  <pre><code>class MyCollection : IEnumerable&lt;MyClass&gt;
  {
      public MyCollection()
      {
      }
  
      public IEnumerator&lt;MyClass&gt; GetEnumerator()
      {
          return null;
      }
  
      System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
      {
          return GetEnumerator();
      }
  }
  </code></pre>
  
  <p>If I do add an implementation of <code>IEnumerable::GetEnumerator()</code>, the compiler complains about two methods that differ only by return type (which also makes sense).</p>
  
  <p>So, how do I implement <code>IEnumerable&lt;T&gt;</code> in a C++/CLI class?</p>