public 摘要 class Foo<T> 其中 T : Foo<T>。 When/why 这对确保 T 实现 Foo<T> 有用吗
public abstract class Foo<T> where T : Foo<T>. When/why is this useful to ensure T implements Foo<T>
我正在研究 C# 中的通用约束,但无法理解以下内容:
public abstract class Foo<T> where T : Foo<T>
使用时
public class Bar : Foo<Bar>
我想找到一个例子来说明为什么使用它以及解决了什么问题,这样我就可以编写自己的例子来帮助我理解它。不幸的是,除了使用它的代码片段之外,我没有找到任何东西,但没有解释,我希望这是因为我不理解它的根本原因 achieves/solves,因此我的搜索字词对我来说没有任何意义。
这就是众所周知的奇怪的重复模板模式。你肯定不是第一个被它弄糊涂的人。它使您能够访问有关基 class 内的派生 class 的信息。本文中有一些很好的用例示例:https://zpbappi.com/curiously-recurring-template-pattern-in-csharp/
我正在研究 C# 中的通用约束,但无法理解以下内容:
public abstract class Foo<T> where T : Foo<T>
使用时
public class Bar : Foo<Bar>
我想找到一个例子来说明为什么使用它以及解决了什么问题,这样我就可以编写自己的例子来帮助我理解它。不幸的是,除了使用它的代码片段之外,我没有找到任何东西,但没有解释,我希望这是因为我不理解它的根本原因 achieves/solves,因此我的搜索字词对我来说没有任何意义。
这就是众所周知的奇怪的重复模板模式。你肯定不是第一个被它弄糊涂的人。它使您能够访问有关基 class 内的派生 class 的信息。本文中有一些很好的用例示例:https://zpbappi.com/curiously-recurring-template-pattern-in-csharp/