C#params关键字
Copyright Notice: This article is an original work licensed under the CC 4.0 BY-NC-ND license.
If you wish to repost this article, please include the original source link and this copyright notice.
Source link: https://v2know.com/article/188
✦ params关键字的参数必须是一维数组,
✦ params声明的一维数组在一个方法只允许出现一次
✦ params声明的参数之后,不允许再出现任何参数,换句话说,其他参数你只能写在前面
✦ params的功能可以用List等集合代替,只是使用Params显得更直观。
举例:
class Program
{
static void Main(string[] args)
{
PrintMany("Hello", "World");
}static void PrintMany(params object[] allParams)
{
if (allParams != null)
{
foreach (var p in allParams)
{
Console.WriteLine(p);
}
}Console.Read();
}
}
输出:
Hello
World
✦ params并不影响方法签名。
This article was last edited at 2020-03-14 06:23:40