Class X: X <T> {} template in C # --- what is it used for?

What is this template used for? note that it differs from C ++ "curiously repeating pattern template".

+5
source share
1 answer

Having a generic ancestral class knows that the actual descendant that inherits from it helps in scenarios where the generic ancestor needs to expose a specific unrelated descendant class as part of the non-native end result of its own contract.

One common example is the 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 main advantage of this concept is code deduplication.

+4
source

All Articles