Unpacking an array into method arguments Unpacking an array into method arguments arrays arrays

Unpacking an array into method arguments


Your method needs a return type:

int Add(params int[] xs) {    return xs.Sum();}

And to call it with an array you just use the ordinary syntax for method calls:

int[] xs = new[] { 1, 2, 3 };var result = Add(xs);


The params keyword basically just allows you to take advantage of a little syntactic sugar. It tells the compiler that when it sees

Add(1, 2, 3);

It should convert that to

Add(new int[] { 1, 2, 3});

So to do this from your code, you don't have to do anything special.

 int[] parameters = new int[] { ... } results = Add(parameters);

See the documentation for more details.


As far as I know, you can just call it with an array like you would a normal method:

Add(xs);

Nothing fancy, no params keyword on the method call, no dots.