[英]2 params in C# not compiling
I'm trying to do this: 我正在尝试这样做:
public void CustomMethod(params int[] number,params string[] names)
{
...
}
If i delete one of them , there is no problems , any idea of why i can't do this? 如果我删除其中一个,就没有问题,为什么我不能这样做呢?
I have tried putting a normal parametre in the middle of both. 我尝试将普通参数置于两者的中间。
Only the last parameter can have params
. 只有最后一个参数可以具有
params
。 See the documentation . 请参阅文档 。
No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.
方法声明中的params关键字之后不允许使用其他参数,而方法声明中仅允许一个params关键字。
The reason is that allowing multiple params would give ambiguity. 原因是允许多个参数会产生歧义。 For example, what would this mean?
例如,这意味着什么?
public void CustomMethod(params int[] foo, params int[] bar)
{
...
}
// ...
CustomMethod(1, 2);
This is simply not supported. 根本不支持此功能。 The compiler can't know when one parameter list ends and the next begins.
编译器无法知道一个参数列表何时结束而下一个参数列表何时开始。
据我所知,您只能在构造函数中编写一个params参数,该参数应该是构造函数的最后一个参数。
The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable. 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. 方法声明中的params关键字之后不允许使用其他参数,而方法声明中仅允许一个params关键字。
See Here : http://msdn.microsoft.com/en-us/library/w5zay9db(v=VS.71).aspx 请参阅此处: http : //msdn.microsoft.com/zh-cn/library/w5zay9db(v=VS.71).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.