Using the "params" keyword for generic parameters in C#

is it possible in C# to specify that a generic type can have any number of type arguments?

No, C# doesn't have anything like that I'm afraid.

Fundamentally Func<T> and Func<T1, T2> are entirely unrelated types as far as the CLR is concerned, and there's nothing like params to specify multiple type arguments.

As for its utility: I can see cases where it could be useful, but I suspect they're rare enough to mean the feature doesn't cross the "benefit/cost" threshold. (Note that it would almost certainly require CLR changes too.)


C++11 has the feature that you're essentially talking about. They call it variadic templates.

C# generics aren't quite like C++ templates, though, and would make it difficult to build quite the same thing.

In the C++ case, the templates are expanded at compile time into whichever concrete types are used. In the C# case, the type specification happens entirely at runtime. And the resulting IL would need to support the number of different types encountered.


No, this cannot be done.

It's not as simple as treating it as an array of types (a concept which doesn't even exist in C#). Consider Func - the number of type parameters must be the same as the number of parameters of the delegate's Invoke method. But how would the programmer express such a relation between type parameters and regular parameters?

However, this feature does exist in C++11 - variadic templates. Note that C++ doesn't allow accessing the individual type parameters using array syntax - instead, functions usually separate the first type parameter from the rest, and use recursive calls to unpack the rest.