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() {
}
}
I don't think you can get it to be the specific subclass. You can do this though:
Your abstract class should be generic.
If you need to refer to the abstract class without the generic type argument, use an interface:
Try this: