Are multiple "params" parameters possible?

Viewed 38268

Is it possible to have multiple params parameters in C#? Something like this:

void foobar(params int[] foo, params string[] bar)

But I'm not sure if that's possible. If it is, how would the compiler decide where to split the arguments?

7 Answers

I know this is a super old post, but here:

In C# 7, you actually can. You can use System.ValueTuple to do this:

private void Foobar(params (int Foo, string Bar)[] foobars)
{
    foreach (var foobar in foobars)
        Console.WriteLine($"foo: {foobar.Foo}, bar: {foobar.Bar}");   
}

And then you can use it like this:

private void Main() => Foobar((3, "oo"), (6, "bar"), (7, "baz"));

And the obvious output:

Foo: 3, Bar: foo
Foo: 6, Bar: bar
Foo: 7, Bar: baz

The only downside is you have to do this: foobars[0].foo; instead of foos[0];, but that's really a tiny tiny issue. Besides, if you really wanted to, you could make some extension or function to convert them to arrays, though I don't think that's really worth it.

Related