garytchao
garytchao

Reputation: 471

Are multiple "params" parameters possible?

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?

Upvotes: 47

Views: 48190

Answers (7)

AustinWBryan
AustinWBryan

Reputation: 3326

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}");   
}

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

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.

EDIT:

Using deconstructers, this becomes the previous "downside" goes away:

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

Upvotes: 26

Samuel Neff
Samuel Neff

Reputation: 74899

You can only have one params argument. You can have two array arguments and the caller can use array initializers to call your method, but there can only be one params argument.

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

...

foobar(new[] { 1, 2, 3 }, new[] { "a", "b", "c" });

Upvotes: 49

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

No, only a single param is allowed and even that has to be the last argument. Read this

This will work

public void Correct(int i, params string[] parg) { ... }

But this won't work

public void Correct(params string[] parg, int i) { ... }

Upvotes: 6

Cole Tobin
Cole Tobin

Reputation: 9429

No this is not possible. Take this:

void Mult(params int[] arg1, params long[] arg2)

how is the compiler supposed to interpret this:

Mult(1, 2, 3);

It could be read as any of these:

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

You can take two arrays as params like this however:

void Mult(int[] arg1, params long[] arg2)

Upvotes: 39

Habib
Habib

Reputation: 223187

From MSDN - params

No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.

Upvotes: 12

Feng Yuan
Feng Yuan

Reputation: 707

void useMultipleParams(int[] foo, string[] bar)
{
}


useMultipleParams(new int[]{1,2}, new string[] {"1","2"}) 

Upvotes: -1

Dmitry Khryukin
Dmitry Khryukin

Reputation: 6438

It's not possible. It could be only one params keyword per method declarations - from MSDN - http://msdn.microsoft.com/en-us/library/w5zay9db(v=vs.71).aspx

Upvotes: 1

Related Questions