开发者

Inheritance in Generics [closed]

开发者 https://www.devze.com 2023-02-06 12:48 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_开发技巧 Closed 12 years ago.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号