views:

548

answers:

1

I have this C# code:

abstract class MyList : IEnumerable<T>
{
    public abstract IEnumerator<T> GetEnumerator();

    //abstract IEnumerator IEnumerable.GetEnumerator();
}

As is, I get:

'Type' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'.

remove the comment and I get:

The modifier 'abstract' is not valid for this item

How do I make an explicit implementation abstract

+11  A: 

Interesting - I'm not sure you can. However, if this is your real code, do you ever want to implement the non-generic GetEnumerator() in any way other than by calling the generic one?

I'd do this:

abstract class MyList<T> : IEnumerable<T>
{
    public abstract IEnumerator<T> GetEnumerator();

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

That saves you from the tedium of having to implement it in every derived class - which would no doubt all use the same implementation.

Jon Skeet
I have no reason the think it will get implement any other way... but you never know. +1
BCS
Re your comment - I may have misread it... edited to fix it, and found the code identical to yours - so have deleted ;-p
Marc Gravell