I have the following structure:
abstract class Base {
public abstract List<...> Get(); //What should be the generic type?
}
class SubOne :
Your abstract class should be generic.
abstract class Base<T> {
public abstract List<T> Get();
}
class SubOne : Base<SubOne> {
public override List<SubOne> Get() {
}
}
class SubTwo : Base<SubTwo> {
public override List<SubTwo> Get() {
}
}
If you need to refer to the abstract class without the generic type argument, use an interface:
interface IBase {
//common functions
}
abstract class Base<T> : IBase {
public abstract List<T> Get();
}
Try this:
public abstract class Base<T> {
public abstract List<T> Foo();
}
public class Derived : Base<Derived> { // Any derived class will now return a List of
public List<Derived> Foo() { ... } // itself.
}
I don't think you can get it to be the specific subclass. You can do this though:
abstract class Base<SubClass> {
public abstract List<SubClass> Get();
}
class SubOne : Base<SubOne> {
public override List<SubOne> Get() {
throw new NotImplementedException();
}
}
class SubTwo : Base<SubTwo> {
public override List<SubTwo> Get() {
throw new NotImplementedException();
}
}
public abstract class Base<T>
{
public abstract List<T> Get();
}
class SubOne : Base<SubOne>
{
public override List<SubOne> Get() { return new List<SubOne>(); }
}
class SubTwo : Base<SubTwo>
{
public override List<SubTwo> Get() { return new List<SubTwo>(); }
}