IEnumerable<T> in .NET 2.0 Beta 2

I’ve started playing with the full version of Visual Studio 2005, the Feb CTP. I’ve abandoned the Express edition because they stripped it of all functionality. No wonder they’re giving it away free. No Add-Ins, no refactorings: do they think that hobbyists and students are idiots and won’t want those features?

Anyway, I had some code that worked with Beta 1, but was broken when I re-compiled in Beta 2. I eventually found that IEnumerable<T> implements IEnumerable in Beta 2. So if you have a class that implements IEnumerable<T>, you’ll get a compiler error that is pretty confusing. Suppose you have a class that implements IEnumerable<T>:

public class ExampleCollection : IEnumerable<Example>
{
  public IEnumerator<Example> GetEnumerator()
  {
    //give examples
  }
}

When you compile in Beta 2, you’ll get the following error: ExampleCollection does not implement interface member ‘System.Collections.IEnumerable.GetEnumerator(). ExampleCollection is either static, not public, or has the wrong return type. At first, I thought: “No shit. Of course I don’t implement interface IEnumerable, I implement the generic one. Stupid computer!” But it just sat there silently mocking me.

Then I looked at the docs, and saw that in Beta 2, the generic IEnumerable<T> inherits from IEnumerable as I mentioned earlier. And, as you all know, if you want to implement interface methods with the same signature that differ by return type, you have to implement one of them explicitly. So adding the following to your class will now be required:

public class ExampleCollection : IEnumerable<Example>
{
  public IEnumerator<Example> GetEnumerator()
  {
    //give examples
  }

  IEnumerator IEnumerable.GetEnumerator()
  {
    return GetEnumerator();
  }
}

Perhaps the compiler error message should be a little more clear on that.

CategoriesUncategorizedTags