C# 中的递归泛型类型参数

Recursive generic type parameter in C#

我需要一些帮助来理解 C# 中的递归泛型。

我遇到了这段代码:

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

public class UserId: Value<UserId>
{
}

我对 where 子句两边都使用 Value<T> 的部分感到困惑。有人可以解释一下代码的作用吗?

它是一个递归泛型类型参数。

表示T必须是T的Value

发现很难理解很正常,我发现很难解释...抱歉。

其他人应该能解释得更好。

Recursive Generics

Recursive Generics Restrictions

它被称为“Curiously recurring template pattern" . C# examples here and here. Often used for fluent syntax 接口类型,以便将通用类型 "known" 保持在基本实现中。