I can't figure out the use for this code. Of what use is this pattern?
[code repeated here for posterity]
public class Turtle<T> where T : Turtle<T>
{
}
This pattern essentially allows you to refer to a concrete subclass within the parent class. For example:
public abstract class Turtle<T> where T : Turtle<T>
{
public abstract T Procreate();
}
public class SeaTurtle : Turtle<SeaTurtle>
{
public override SeaTurtle Procreate()
{
// ...
}
}
Versus:
public abstract class Turtle
{
public abstract Turtle Procreate();
}
public class SnappingTurtle : Turtle
{
public override Turtle Procreate()
{
// ...
}
}
In the former, it's specified that a SeaTurtle
's baby will be a SeaTurtle
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With