To accept a varied amount of arguments, you will have to add method overloads with different numbers of generic type parameters. Only signatures are shown below.
VTransform<T,U,V>(Func<T,U,V>transformFn,Tt,Uu);WTransform<T,U,V,W>(Func<T,U,V,W>transformFn,Tt,Uu,Vv);XTransform<T,U,V,W,X>(Func<T,U,V,W,X>transformFn,Tt,Uu,Vv,Ww);// etc.
Note that some of the types T, U, V, etc could actually be the same type. Different letters are used in case they are different.
The above code does not make sense to use in practice, but it demonstrates the concept.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yes you can.
The type for a function that returns void is Action<T> or with multiple parameters,
Action<T1, T2, ...>
.The type for a function that returns a value is Func<T, TReturn> or with multiple parameters
Func<T1, T2, ..., TReturn>
.So if you want to define a method that takes in a function and an
int
, it could look like this.PlusOne
could easily be a one-liner, but made it multiple lines for demonstration purposes.But what if I want to accept ANY kind of function? With a custom amount of args, and a custom return value?
Custom return value is easy. You just have to add a generic parameter(s) to the function.
To accept a varied amount of arguments, you will have to add method overloads with different numbers of generic type parameters. Only signatures are shown below.
Note that some of the types T, U, V, etc could actually be the same type. Different letters are used in case they are different.
The above code does not make sense to use in practice, but it demonstrates the concept.