多类型参数约束的 C# 泛型语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/965580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# generics syntax for multiple type parameter constraints
提问by Jon B
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)
你怎么做到这一点?(使用 .NET 2)
采纳答案by Joel Martinez
void foo<TOne, TTwo>()
where TOne : BaseOne
where TTwo : BaseTwo
More info here:
http://msdn.microsoft.com/en-us/library/d5x73970.aspx
更多信息在这里:http:
//msdn.microsoft.com/en-us/library/d5x73970.aspx