简体   繁体   English

如何在C#中为泛型类型指定多个约束?

[英]How do I specify multiple constraints on a generic type in C#?

What is the syntax for placing constraints on multiple types? 在多种类型上放置约束的语法是什么? The basic example: 基本的例子:

class Animal<SpeciesType> where SpeciesType : Species

I would like to place constraints on both types in the following definition such that SpeciesType must inherit from Species and OrderType must inherit from Order : 我想放在这两种类型的约束如下定义,使得SpeciesType必须继承SpeciesOrderType必须继承Order

class Animal<SpeciesType, OrderType>
public class Animal<SpeciesType,OrderType>
    where SpeciesType : Species
    where OrderType : Order
{
}

You should be able to go : 你应该可以去:

class Animal<SpeciesType, OrderType>
    where SpeciesType : Species
    where OrderType : Order {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM