C# 是否可以使用多个“params”参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11838709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are multiple "params" parameters possible?
提问by garytchao
Is it possible to have multiple paramsparameters in C#? Something like this:
paramsC#中可以有多个参数吗?像这样的东西:
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?
但我不确定这是否可能。如果是,编译器将如何决定在哪里拆分参数?
回答by Samuel Neff
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.
您只能有一个 params 参数。您可以有两个数组参数,调用者可以使用数组初始值设定项来调用您的方法,但只能有一个 params 参数。
void foobar(int[] foo, string[] bar)
...
foobar(new {1, 2,3}, new {"a", "b", "c"});
回答by Cole Johnson
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)
回答by Nikhil Agrawal
回答by Dmitry Khryukin
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
这是不可能的。每个方法声明可能只有一个 params 关键字 - 来自 MSDN - http://msdn.microsoft.com/en-us/library/w5zay9db(v=vs.71).aspx
回答by Feng Yuan
void useMultipleParams(int[] foo, string[] bar)
{
}
useMultipleParams(new int[]{1,2}, new string[] {"1","2"})
回答by Habib
From MSDN - params
No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permittedin a method declaration.
方法声明中的 params 关键字后不允许附加参数,并且方法声明中只允许一个 params 关键字。
回答by AustinWBryan
I know this is a super old post, but here:
我知道这是一个超级旧的帖子,但在这里:
In C# 7, you actually can. You can use System.ValueTupleto do this:
在 C# 7 中,您实际上可以。您可以使用以下System.ValueTuple方法执行此操作:
private void Foorbar(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.
唯一的缺点是您必须这样做:foobars[0].foo;而不是foos[0];,但这确实是一个很小的问题。此外,如果你真的愿意,你可以做一些扩展或函数来将它们转换为数组,尽管我认为这并不值得。

