Recursive generic type parameter in C#
Asked Answered
C

2

5

I needed some help understanding recursive generics in C#.

I came across this code:

public abstract class Value<T> where T : Value<T>
{
    ....
}

public class UserId: Value<UserId>
{
}

I am confused by the part where the Value<T> is used on both sides of the where clause. Can someone please explain what the code does?

Clower answered 8/10, 2019 at 18:36 Comment(9)
It's a generic type constraint learn.microsoft.com/en-us/dotnet/csharp/programming-guide/…Belloir
This is C# generic syntax. Its saying that the abstract class Value takes a generic type parameter and that parameter is represented by the variable T. The where means that there is a restriction where T has to be a type that implements Value<T>. @Belloir has linked the docs which are more detailed, my comment is a quick summaryShantay
These are simple c# generics. You can read about them here (general generics) and here (generic type constraints). If that's not your question you might need to clarify what exactly you don't understand. EDIT: While typing that I got beat by many others so sorry for the repetition :)Sloat
@Shantay I am a bit confused about the part where Value<T> is used on both sides of the where clause. Isnt that recursive?Clower
@Clower that part does look a little weird to me. I assumed it compiled/ran fine though since you didnt mention anything to the contrary. If your question is about that specifically I think you should edit and re-word your question so a proper answer/discussion can take placeShantay
It's known as a "Curiously recurring template pattern" (en.wikipedia.org/wiki/Curiously_recurring_template_pattern). C# examples zpbappi.com/curiously-recurring-template-pattern-in-csharp and blog.arkanosoft.com/index.php/crtp-cSauerbraten
@Shantay Yes, it compiles. The code is from here: github.com/PacktPublishing/…Clower
@JesseC.Slicer Thank you for the links. They were very helpful. Also, the name gave me something to google more specifically and I learnt something new. If you can submit that as an answer, I would be happy to accept it. Thank you again.Clower
I know this is an old thread, but worth mentioning - as of C# 9, covariant returns can be used in lieu of CRTP in most cases.Lanfri
S
6

It's known as a "Curiously recurring template pattern" . C# examples here and here. Often used for fluent syntax of interface types in order to keep the generic type "known" to the base implementation.

Sauerbraten answered 8/10, 2019 at 18:59 Comment(0)
C
2

It a recursive generic type parameter.

It means that T must be a Value of T.

It is normal to found that difficult to understand and I found it is difficult to explain... sorry.

Someone else should be able to explain better.

Recursive Generics

Recursive Generics Restrictions

Complected answered 8/10, 2019 at 18:49 Comment(1)
While this is fine, since you've named the pattern so users can find articles themselves, answers with links to explanations are generally frowned upon. It's better to quote the relevant parts of the articles (if the license is amenable).Kowal

© 2022 - 2025 — McMap. All rights reserved.