C#泛型多个类型参数约束语法 [英] C# generics syntax for multiple type parameter constraints

查看:2962
本文介绍了C#泛型多个类型参数约束语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  泛型方法和多种约束


我需要一个有两个类型约束的泛型函数,分别从不同的基类继承。我知道如何用一种类型做到这一点:

 无效美孚< T>()其中T:BaseClass的

不过,我不知道如何使用两种类型的做到这一点:

 无效美孚<口吻,TTWO>()式音:// BaseOne TTWO和:BaseTwo?

你怎么做到这一点? (使用.NET 2)


解决方案

 无效美孚<口吻,TTWO>()
   乐音:BaseOne
   其中,TTWO:BaseTwo

在这里更多信息:

<一href=\"http://msdn.microsoft.com/en-us/library/d5x73970.aspx\">http://msdn.microsoft.com/en-us/library/d5x73970.aspx

Possible Duplicate:
Generic methods and multiple constraints

I need a generic function that has two type constraints, each inheriting from a different base class. I know how to do this with one type:

void foo<T>() where T : BaseClass

However, I don't know how to do this with two types:

void foo<TOne, TTwo>() where TOne : BaseOne // and TTwo : BaseTwo ???

How do you do this? (using .NET 2)

解决方案

void foo<TOne, TTwo>() 
   where TOne : BaseOne
   where TTwo : BaseTwo

More info here:
http://msdn.microsoft.com/en-us/library/d5x73970.aspx

这篇关于C#泛型多个类型参数约束语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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