【发布时间】:2017-09-05 07:56:55
【问题描述】:
假设我有一个接口 IA,其中包含一个名为 Foo 的通用方法。
public interface IA {
int Foo<T>(T otherType);
}
我希望 T 与派生类的类型相同:
class A : IA {
int Foo(A otherType)
{
}
}
我尝试了以下(语法错误):
public interface IA {
int Foo<T>(T otherType) where T : this;
}
我的约束需要如何实现?
【问题讨论】:
-
不要认为那是可能的。接口不知道什么会实现它。这基本上是鸡/蛋的事情。
标签: c# generics inheritance interface constraints