Note that there are some explanatory texts on larger screens.

plurals
  1. POAbstract base class that inherits ICollection<T>
    text
    copied!<p>Suppose I have an abstract base class <code>BaseTimeCollection</code> and at least one concrete class <code>ConcreteTimeCollection</code> which inherits from the base class.</p> <p>I would like my base class to inherit from <code>ICollection&lt;T&gt;</code>. </p> <p>Inheriting from <code>ICollection&lt;T&gt;</code> requires me to provide implementations for a number of methods, including <code>IEnumerable.GetEnumerator()</code> and <code>IEnumerable&lt;T&gt;.GetEnumerator</code>.</p> <p>I do not want to implement these methods in <code>BaseTimeCollection</code> - instead I would prefer to implement them individually in each of my concrete classes. </p> <p>This is where I run into trouble.</p> <p>The <code>GetEnumerator</code> methods need to be declared explicitly, because there are two of them both with the same name but different return types. However, it seems that as soon as I make the signature explicit I can no longer use the abstract modifier. Essentially I am forced to implement the <code>GetEnumerator</code> method in my base class. </p> <pre><code>public abstract class BaseTimeCollection : ICollection&lt;Time&gt; { abstract IEnumerator IEnumerable.GetEnumerator(); // compile error: The modifier 'abstract' is not valid for this item abstract IEnumerator&lt;Time&gt; IEnumerable&lt;Time&gt;.GetEnumerator(); // compile error: The modifier 'abstract' is not valid for this item } public class ConcreteTimeCollection : BaseTimeCollection { IEnumerator IEnumerable.GetEnumerator() { // this is where I would like to provide my implementation for IEnumerable.GetEnumerator() } IEnumerator&lt;Time&gt; IEnumerable&lt;Time&gt;.GetEnumerator() { // this is where I would like to provide my implementation for IEnumerable&lt;Time&gt;.GetEnumerator() } } </code></pre> <ol> <li><p>What have I missed? </p></li> <li><p>Is there some way I can defer implementation of the <code>GetEnumerator</code> methods to the concrete classes?</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload