[英]C# generics and subclassing - why must I not give the type parameter on ctor? (simple)
I have我有
public class LDBList<T> : List<T> where T : LDBRootClass {
public LDBList<T>(LDBList<T> x) : base(x) { } // not allowed
public LDBList (LDBList<T> x) : base(x) { } // allowed
...
}
Why is the generic type not accepted on the constructor?为什么构造函数不接受泛型类型?
The language does not support generic constructors.该语言不支持泛型构造函数。 Have a look at the grammar for constructors :看看构造函数的语法:
constructor_declaration
: attributes? constructor_modifier* constructor_declarator constructor_body
;
Compare that with the grammar for methods :将其与方法的语法进行比较:
method_declaration
: method_header method_body
;
method_header
: attributes? method_modifier* 'partial'? return_type member_name type_parameter_list?
'(' formal_parameter_list? ')' type_parameter_constraints_clause*
;
The grammar for methods includes a type_parameter_list
, and a type_parameter_constraints_clause
, which the grammar for constructors does not.方法的语法包括type_parameter_list
和type_parameter_constraints_clause
,而构造函数的语法没有。
Even if it did, it is unclear what your code means, because you are declaring a type parameter with the same name as an existing type parameter - the T
declared in the class.即使这样做了,也不清楚您的代码是什么意思,因为您正在声明一个与现有类型参数同名的类型参数 - 在类中声明的T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.