更新:2007 年 11 月
params 关键字可以指定在参数数目可变处采用参数的
在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。
示例
C# | 复制代码 |
---|---|
public class MyClass { public static void UseParams(params int[] list) { for (int i = 0 ; i < list.Length; i++) { Console.Write(list[i] + " "); } Console.WriteLine(); } public static void UseParams2(params object[] list) { for (int i = 0 ; i < list.Length; i++) { Console.Write(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); } } /* Output: 1 2 3 1 a test 10 11 12 */ |
C# 语言规范
有关更多信息,请参见 C# 语言规范中的以下各章节:
10.6.1.4 参数数组