Search code examples
c#genericsabstract-class

How to define a generic abstract class that inherits a generic abstract class?


I have a class that inherits an abstract class which inherits another abstract class. I can define the most-super class as being generic just fine, but I don't know how to define the middle class as being generic before I express the actual type I want in the final ListResults class.

internal abstract class LowerCommand<T> {
    public abstract void Execute();
    public abstract List<T> ExecuteList();
}

// currently gives error for `T`
internal abstract class HigherCommand : Lowercommand<T> {
    // ... defines other stuff, nothing to do with
    //     already instantiated methods or T ...
}

class ListResults : HigherCommand<Results>
{
    public override void Execute() {...}
    public override List<Results> ExecuteList() {...}
}

Thanks for your help.


Solution

  • You still need to define the generic type parameter T on the definition of HigherCommand so it can in turn properly define LowerCommand.

    internal abstract class LowerCommand<T> {
        public abstract void Execute();
        public abstract List<T> ExecuteList();
    }
    
    // Note that HigherCommand require a declaration of `T`
    internal abstract class HigherCommand<T> : LowerCommand<T> {
        // ... defines other stuff, nothing to do with
        //     already instantiated methods or T ...
    }
    
    class ListResults : HigherCommand<Results>
    {
        public override void Execute() {...}
        public override List<Results> ExecuteList() {...}
    }