params 构造函数声明数组 而不知道数组长度 用的
在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。
using System;
public class MyClass
{
public static void UseParams(params int[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}
public static void UseParams2(params object[] list)
{
for (int i = 0 ; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}
static void Main()
{
UseParams(1, 2, 3);
UseParams2(1, 'a', "test");
// An array of objects can also be passed, as long as
// the array type matches the method being called.
int[] myarray = new int[3] {10,11,12};
UseParams(myarray);
}
}
输出
1
2
3
1
a
test
10
11
12
C#中的params关键字的用法
最新推荐文章于 2024-11-04 18:00:00 发布
本文介绍了在C#中使用params关键字处理方法的可变参数,包括声明方法、处理数组以及不同类型的参数传递。通过示例展示了如何在方法中使用params关键字,并解释了其在实际开发中的应用。
1526

被折叠的 条评论
为什么被折叠?



