C# 泛型:约束 T,其中 T:对象无法编译;错误:约束不能是特殊类“对象”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10644703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Generics: Constraining T where T : Object doesn't compile; Error: Constraint cannot be special class 'object'
提问by goodguys_activate
When I constrain T with : Object like this:
当我用 : Object 约束 T 时:
public interface IDoWork<T> where T : Object
{
T DoWork();
}
I get the error:
我收到错误:
Constraint cannot be special class 'object'
约束不能是特殊类“对象”
Does that mean there is an implied difference with the following that does compile?
这是否意味着与以下编译的内容存在隐含的差异?
public interface IDoWork<T> // where T : Object
{
T DoWork();
}
采纳答案by Chris Hannon
There is no difference between the two constraints, except for that one is disallowed for being useless to explicitly state.
这两个约束之间没有区别,除了一个是不允许明确声明的。
The C# 4.0 language specification (10.1.5 Type parameter constraints) says two things about this:
C# 4.0 语言规范(10.1.5 类型参数约束)对此说了两件事:
The type must not be object. Because all types derive from object, such a constraint would have no effect if it were permitted.
...
If T has no primary constraints or type parameter constraints, its effective base class is object.
类型不能是对象。因为所有类型都从对象派生,所以如果允许的话,这样的约束将不起作用。
...
如果 T 没有主约束或类型参数约束,则其有效基类为 object。
In your comment, you said that you were trying to make Tbe of type Void. Voidis a special type that indicates that there is no return type and cannot be used in place of T, which requires an appropriate concrete type. You will have to create a void version of your method and a Tversion if you want both.
在你的评论中,你说你试图T成为类型Void。Void是一种特殊类型,表示没有返回类型,不能代替T,需要一个合适的具体类型。T如果您想要两者,您将不得不创建您的方法的无效版本和一个版本。
回答by Douglas
If you want to constrain a generic type to be a reference type, use : class.
如果要将泛型类型限制为引用类型,请使用: class.
public interface IDoWork<T> where T : class
{
T DoWork();
}
This will forbid the generic type from being a value type, such as intor a struct.
这将禁止泛型类型成为值类型,例如int或 结构。
回答by Rahul Nikate
As per C# 4.0 Language Specification (Coded : [10.1.5] Type parameter constraints) tells two things:
根据 C# 4.0 语言规范(编码:[10.1.5] 类型参数约束)说明了两件事:
1] The type must not be object. Because all types derive from object, such a constraint would have no effect if it were permitted.
2] If T has no primary constraints or type parameter constraints, its effective base class is object.
1] 类型不能是对象。因为所有类型都从对象派生,所以如果允许的话,这样的约束将不起作用。
2] 如果 T 没有主约束或类型参数约束,则其有效基类为 object。
When you define a generic class, you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates your class. If client code tries to instantiate your class by using a type that is not allowed by a constraint, the result is a compile-time error. These restrictions are called constraints. Constraints are specified by using the where contextual keyword. If you want to constrain a generic type to be a reference type, use : class.
定义泛型类时,可以对客户端代码在实例化类时可用于类型参数的类型应用限制。如果客户端代码尝试使用约束不允许的类型来实例化您的类,则结果是编译时错误。这些限制称为约束。使用 where 上下文关键字指定约束。 如果要将泛型类型限制为引用类型,请使用 :class。
According to MSDN
根据MSDN
Constraint cannot be special class 'identifier'. The following types may not be used as constraints:
约束不能是特殊类“标识符”。以下类型不能用作约束:
- System.Object
- System.Array
- System.Delegate
- System.Enum
- System.ValueType.
- 系统对象
- 系统数组
- 系统.委托
- 系统枚举
- 系统值类型。

