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.

Tags:

C#

Generics