当我用这样的对象约束 T 时:
public interface IDoWork<T> where T : Object
{
T DoWork();
}
我得到错误:
Constraint cannot be special class 'object'
这是否意味着与编译后的以下内容存在隐含差异?
public interface IDoWork<T> // where T : Object
{
T DoWork();
}
最佳答案
如果要将泛型类型限制为引用类型,请使用: class
。
public interface IDoWork<T> where T : class
{
T DoWork();
}
这将禁止泛型成为值类型,例如 int
或结构。
关于C# 泛型 : Constraining T where T : Object doesn't compile; Error: Constraint cannot be special class 'object' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10644703/