【问题标题】:What's the syntax to derive a generic class from another generic class when both have constraints?当两者都有约束时,从另一个泛型类派生泛型类的语法是什么?
【发布时间】:2020-05-24 11:27:15
【问题描述】:

我有一个基本的泛型类定义如下:

public class TheBaseClass<T> where T : Interface1 {}

我想从 TheBaseClass 派生另一个泛型类。定义如下:

public class SomeClass<T> where T : Interface2 {}

我查看了文档,但找不到可以编译的示例。我们将不胜感激地收到所有建议。

【问题讨论】:

  • 你不能改变基类的约束。这两个接口之间有什么关系吗?

标签: c# class generics derived


【解决方案1】:

在继承泛型基类时,还应为泛型类型参数指定基类约束:

public class SomeClass<T> : TheBaseClass<T>
 where T : Interface1, Interface2
{}

【讨论】:

  • 感谢您惊人的快速和准确的回复。干杯。
猜你喜欢
  • 1970-01-01
  • 2011-08-31
  • 2016-06-28
  • 2023-03-06
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 2014-03-22
  • 2019-06-27
相关资源
最近更新 更多