[英]Inherited generic as a function parameter
我怎么能做到这一点?
class A { }
class B : A { }
class X<T> where T : A { }
class Y<T> : X<T> where T : A { }
private static void f(X<A> x) { }
public static void Main(string[] args)
{
Y<B> y = new Y<B>();
f(y); // Compiler error here
}
Y从A继承自X,B,但它没有被编译。
将函数定义更改为:
private static void f<T>(X<T> x) where T : A { }
正如您所定义的那样,您说f()
必须传递X<A>
的实例。 通过定义我在这里显示,你说f()
接受任何具有X<A>
作为父类的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.