如何在C#中对泛型类型指定多个约束?
时间:2020-03-05 18:48:23 来源:igfitidea点击:
对多种类型施加约束的语法是什么?基本示例:
class Animal<SpeciesType> where SpeciesType : Species
我想在以下定义中对这两种类型设置约束,以使SpeciesType必须从Species继承而OrderType必须必须从Order继承:
class Animal<SpeciesType, OrderType>
解决方案
回答
public class Animal<SpeciesType,OrderType> where SpeciesType : Species where OrderType : Order { }
回答
我们应该可以:
class Animal<SpeciesType, OrderType> where SpeciesType : Species where OrderType : Order { }