I have the following structure:
abstract class Base {
public abstract List<...> Get(); //What should be the generic type?
}
class SubOne : Base {
public override List<SubOne> Get() {
}
}
class SubTwo : Base {
public override List<SubTwo> Get() {
}
}
I want to create an abstract method that returns whatever class the concrete sub class is. So, as you can see from the example, the method in SubOne
should return List<SubOne>
whereas the method in SubTwo
should return List<SubTwo>
.
What type do I specify in the signature declared in the Base class ?
[UPDATE]
Thank you for the posted answers.
The solution is to make the abstract class generic, like such:
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() {
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…