I have the following code that creates an instance of an anonymous type having a method as the only member:
var test = new {
f = new Func<int, int>(x => x)
};
I want to implement a function that sums up all of its parameters, regardless of how many are passed. This is how a normal method would look like:
int Sum(params int[] values) {
int res = 0;
foreach(var i in values) res += i;
return res;
}
However, I don´t know if this would work for anonymous methods. I tried out Func<params int[], int>
, but obviously that won´t compile. Is there any way to write an anonymous method with a variable list of parameters, or at least with optional args?
EDIT: What I´d like to achieve is to call the (anonymous) sum-method like this: test.Sum(1, 2, 3, 4)
.