Reputation: 5
I am looking into generic constraints in c# and am having trouble understanding the following:
public abstract class Foo<T> where T : Foo<T>
when used
public class Bar : Foo<Bar>
I would like to find an example of why it is used and what is solves so i can make up my own examples to help me understand it. Unfortunately, I am failing to find anything except code snippets where it is used but without explanation which I expect is due to me not understanding what it fundamentally achieves/solves, hence my search terms are returning nothing meaningful for me.
Upvotes: 0
Views: 312
Reputation: 10874
This is known as the Curiously Recurring Template Pattern. You're certainly not the first person to be confused by it. It enables you to access information about the derived class inside the base class. There are some good examples of use cases in this article: https://zpbappi.com/curiously-recurring-template-pattern-in-csharp/
Upvotes: 2