abstract explicit interface implementation in C#
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.
While an explicit interface member may not be abstract (or virtual), it may be implemented in terms of an abstract (or virtual) member1:
public abstract class Foo: IEnumerable {
IEnumerator IEnumerable.GetEnumerator() {
return getEnumerator();
}
protected abstract IEnumerator getEnumerator();
}
public class Foo<T>: Foo, IEnumerable<T> {
private IEnumerable<T> ie;
public Foo(IEnumerable<T> ie) {
this.ie = ie;
}
public IEnumerator<T> GetEnumerator() {
return ie.GetEnumerator();
}
protected override IEnumerator getEnumerator() {
return GetEnumerator();
}
//explicit IEnumerable.GetEnumerator() is "inherited"
}
I've found the need for this in strongly typed ASP.NET MVC 3 partial views, which do not support generic type definition models (as far as I know).