首页 > 其他分享 >class C<T> where T : new() 泛型类约束

class C<T> where T : new() 泛型类约束

时间:2023-03-10 15:46:33浏览次数:39  
标签:约束 类型 泛型 new where class 构造函数

class C<T> where T : new()

这是类型参数约束,where表明了对类型变量T的约束关系。where T:A 表示类型变量是继承于A的,或者是A本省。where T: new()指明了创建T的实例应该使用的构造函数。

 

.NET支持的类型参数约束有以下五种:

where T : struct T必须是一个结构类型
where T : class T必须是一个类(class)类型,不是结构(structure)类型
where T : new() T必须要有一个无参构造函数
where T : NameOfBaseClass T必须继承名为NameOfBaseClass的类
where T : NameOfInterface T必须实现名为NameOfInterface的接口

 转 https://www.cnblogs.com/BluceLee/p/12991652.html

标签:约束,类型,泛型,new,where,class,构造函数
From: https://www.cnblogs.com/wl-blog/p/17203549.html

相关文章