I was checking in the c# 5,6 specs and is not clear if it is possible use a undefined number of types, such as an array, in the signature of a Generic class definition. As it is very know yu can use out paramts <Generics>[] inputArrays
for the constructor signature or any function and the compiler points each parameter to any position in the params array
.
//Using only a genereic type from A class
Foo<A> fooA = new Foo<A>(new A());
//Using two genereic types A and B.
Foo<A,B> fooAB = new Foo<A,B>(new A() , new B() );
//Using three genereic types A ,B and C
Foo<A,B,C> fooABC = new Foo<A,B,C>(new A() , new B(), new C() );
//Using n genereic types such as A , B , C , D , ... , Z
Foo<A,B,C,...,Z> fooAZ = new Foo<A,B,C,..,Z>(new A() , new B(), new C() , ... , new Z() );
The concret question is hot to set properly the correct generic signature to allow an undefined number of Types?
//The signature<T1> only allows one Type in this case.
//The compiler does not like something like this: T:[]
public sealed class Foo<T> {
public Foo<T>(out params T[] args) {
}
}
To avoid this question will be wrong closed I attache the argument that validates that c# could allow it:
using the grammar of csharp
type_parameter_constraints_clauses
: type_parameter_constraints_clause
| type_parameter_constraints_clauses type_parameter_constraints_clause
;
type_parameter_constraints_clause
: 'where' type_parameter ':' type_parameter_constraints
;
type_parameter_constraints
: primary_constraint
| secondary_constraints
| constructor_constraint
| primary_constraint ',' secondary_constraints
| primary_constraint ',' constructor_constraint
| secondary_constraints ',' constructor_constraint
| primary_constraint ',' secondary_constraints ',' constructor_constraint
;
primary_constraint
: class_type
| 'class'
| 'struct'
;
secondary_constraints
: interface_type
| type_parameter
| secondary_constraints ',' interface_type
| secondary_constraints ',' type_parameter
;
constructor_constraint
: 'new' '(' ')'
;
In according to the standard. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#1425-type-parameter-constraints
in the part (1425 of the c# specification) says:
"If C is an array type E[] then Cₓ is the array type Eₓ[]."
it means that you must be use something like :
class Foo<T[]>
Where is the interpretation error in the c# grammar?