c#泛型约束 (where T:class)

时间:2023-03-09 18:09:10
c#泛型约束 (where T:class)

.NET支持的类型参数约束有以下五种:
where T : struct                               | T必须是一个结构类型
where T : class                                 | T必须是一个Class类型
where T : new()                               | T必须要有一个无参构造函数
where T : NameOfBaseClass              | T必须继承名为NameOfBaseClass的类
where T : NameOfInterface               | T必须实现名为NameOfInterface的接口

示例:

public class Myclass<T,U> where T:class where U:struct