继承与C#泛型,而类的类型是继承 [英] inheritance with c# generic, while class type is inherited

查看:105
本文介绍了继承与C#泛型,而类的类型是继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设我有这个:

  public class T:U 
{
...
}

,我想要这个:

  public class A< T> :B< U> 
{
...
}

让我可以在我的代码中有这个:

  B< U> x = new A< T>(); 


解决方案

你不可能完全像你写的那样,但您可以这样做:

  public class A< T,U> :B< U> T:U 
{
...
}

然后做

  B< U> x = new A< T,U>(); 


is something like this possible in C#?

Suppose that I have this:

public class T : U
{
 ...
}

and I want this:

public class A<T> : B<U>
{
...
}

so that I can have this in my code:

B<U> x = new A<T>();

解决方案

You can't have it exactly like you write it, but you can do this:

public class A<T, U> : B<U> where T : U
{
   ...
}

and then do

B<U> x = new A<T, U>();

这篇关于继承与C#泛型,而类的类型是继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆