What is this pattern used for ? note that it is different than the C++ "curiously recurring template pattern".
Having the generic ancestor class know the actual descendant that inherits from it helps in scenarios where the generic ancestor needs to expose a particular non-generic descendant class as part of the non-generic descendant's result own contract.
One common example is a factory method declared in the generic ancestor:
public class Parent<T>
where T : Parent<T>, new
{
public static T Create()
{
return new T(); // would be typically something more sophisticated
}
}
public class Child : Parent<Child>
{
}
The primary advantage of this concept is code-deduplication.
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