Simulate variadic templates in C#

前端 未结 6 951
轮回少年
轮回少年 2020-12-09 14:57

Is there a well-known way for simulating the variadic template feature in C#?

For instance, I\'d like to write a method that takes a lambda with an arbitrary set of p

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 15:46

    C# generics are not the same as C++ templates. C++ templates are expanded compiletime and can be used recursively with variadic template arguments. The C++ template expansion is actually Turing Complete, so there is no theoretically limit to what can be done in templates.

    C# generics are compiled directly, with an empty "placeholder" for the type that will be used at runtime.

    To accept a lambda taking any number of arguments you would either have to generate a lot of overloads (through a code generator) or accept a LambdaExpression.

提交回复
热议问题