c# - 接口(interface) <T> 其中 T : class

标签 c# generics

<分区>

interface<T> where T : class

例如

public interface iSend<T> where T : class

上面的代码是什么意思?

为什么要用这个?

什么时候使用这个?

最佳答案

在这里查看我的完整帖子:Constrain on custom generic type其中讨论了不同类型的通用约束

引用类型约束

约束确保类型参数是引用类型。即类、接口(interface)、委托(delegate)、数组等。

interface iSend<T> where T : class

例子

Valid             InValid
A<MyClass>        A<int>
A<InterfaceME>    A<float>   
A<float[]>  

注意: 应用多个约束时始终排在第一位。

关于c# - 接口(interface) <T> 其中 T : class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21778575/

相关文章:

c# - 编码的 ui 控件轮询而不是等待控件存在

Java 字节码签名

Delphi:泛型和 'is' - 运算符问题

c# - 检查组合框值是否为空

c# - 如何减少通过构造函数传递 IUnityContainer 对象的数量?

java - 你如何比较java中相同泛型类型的两个值?

c# - 使用泛型转换代码

generics - 为什么我会收到错误 "Expected type parameter, found integral variable"?

c# - 使用二进制列作为比较标准

c# - 多线程同步列表<T>