Inherited shared parameter as function parameter
How can i do this?
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 inherited from X, B from A, but it won't compile.
+3
Yola
source
to share
1 answer
Change the function definition to:
private static void f<T>(X<T> x) where T : A { }
As you have defined this, you are saying that an f()
instance should be passed X<A>
. By defining, as I've shown here, you are saying that you f()
accept any class with X<A>
as a parent.
+6
David Arno
source
to share