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?
Value
takes a generic type parameter and that parameter is represented by the variableT
. Thewhere
means that there is a restriction whereT
has to be a type that implementsValue<T>
. @Belloir has linked the docs which are more detailed, my comment is a quick summary – Shantay