Unpacking an array into method arguments
Asked Answered
G

5

11

As you know C# supports variadic methods through the params keyword:

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

Which can then be called with any number of arguments you like:

Add(1);
Add(1, 2);
Add(1, 2, 3);

But say I want to call Add using an array of ints1. Is this possible and how (preferably without reflection)? I tried the following but they gave syntax errors (the syntax was pure guessing):

var xs = new[] { 1, 2, 3 };
Add(xs...); // doesn't work; syntax error
Add(params xs); // doesn't work; syntax error

1 My actual use-case is different but I thought this example would be less complicated.

Gardal answered 20/2, 2013 at 15:27 Comment(7)
What do you mean by doesn't work?Burial
@Antonijn syntax error.Gardal
#36850 This thread is saying it should work. Could the simplification be hiding something?Acridine
Can you define "doesn't work"? Do you get a compile-time error, run-time error, or unexpected output?Jourdan
You are returning something in a method that doesn't return anything (void)Scintillate
Your example is invalid, because you can actualy invoke Add method with an array of int.Bulter
You defined your method void and do you want to return int ?Trawick
H
15

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);
Hoop answered 20/2, 2013 at 15:30 Comment(2)
What would happen if Add takes object[]? Any type is converible to object, so wouldn't that be ambiguous?Gardal
you actually need to cast the inner objects. you could try var xsAsObjcet = xs.ToList.Cast<object>().ToArray;Acridine
S
2

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.

Sacrum answered 20/2, 2013 at 15:33 Comment(1)
use @params for variable name which is a C# keyword. This won't compileClubman
B
0

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.

Burial answered 20/2, 2013 at 15:30 Comment(0)
H
0
static void Main(string[] args)
{
    int[] tmp =  {1, 2};
    var sum = Add(tmp);
}
public static int Add(params int[] xs)
{
    return xs.Sum();
}

Should work just fine..

Halfsole answered 20/2, 2013 at 15:31 Comment(0)
O
0

If it's anything like Java, you can just call the method with the array as an argument.

This feature is also what makes varargs dangerous, especially if one of the vararg types is also an array...

Outdate answered 20/2, 2013 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.