这个东西也是声明函数的时候的一个关键字,但是和ref,out不太一样。
这个东西最大的好处就是,可以不知道这种类型的参数的个数,而动态的调用他。
比如这个函数,下面的这两种的usage都可以。
UseParams(1, 2, 3);
UseParams(1, 2, 3,4 ) ;
The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.
No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.
Example
// cs_params.cs 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(); } public static void Main() { UseParams(1, 2, 3); UseParams2(1, 'a', "test"); int[] myarray = new int[3] {10,11,12}; UseParams(myarray); } }
Output
1 2 3 1 a test 10 11 12