Inheritance in Generics [closed]
How would you make B inherit from A with the following classes?
public class A<U>
{
}
public class B<T>
{
}
You can inherit like this:
public class B<T>:A<T>
{
}
This substitutes the genetic parameter T
of B
as the generic parameter U
of A
. The generic parameters of the base class and derived class are independent. The derived class just needs to specify the parameters it wants in the base class using any types it can name. This includes type-parameters, normal types,... For example you could have List<KeyValuePair<int,T>>
as base-class if you wanted.
Note that the generic parameter T
of B
needs to have enough generic constraints to fulfill the generic constraints of the generic parameter U
of A
.
Does this work for you?
public class A<U>
{ }
public class B<T,U> : A<U>
{ }
Simple as it is
public classB<T>:A<T> { }
Here's a good reference on this
http://msdn.microsoft.com/en-us/library/sz6zd40f%28v=vs.80%29.aspx
精彩评论