在C#中,'where T : class' 是什么意思?

173
在C#中,where T : class是什么意思?
例如:
public IList<T> DoThis<T>() where T : class
11个回答

0
public class MyGenericClass<T> where T : SomeType
{
    // Class implementation
}

类型约束:您可以对类型参数应用各种约束,例如:
类约束(where T : class):确保类型参数必须是引用类型(类或接口)。
结构约束(where T : struct):确保类型参数必须是值类型(结构)。
构造函数约束(where T : new()):确保类型参数必须具有无参数构造函数。
接口约束(where T : IMyInterface):确保类型参数必须实现特定接口。
基类约束(where T : MyBaseClass):确保类型参数必须继承自特定的基类。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接