Description
Use the params keyword to specify a method parameter that takes a variable number of arguments.
You can send a comma-separated list of parameters of the type specified in the parameter declaration or an array of parameters of the specified type. You can also not send parameters. If no parameters are sent, the params list is zero in length.
No additional arguments are allowed after the params keyword in the method declaration, and only one params keyword is allowed in the method declaration.
Example
Public classmyclass{ Public Static void Useparams(params int[] list) { for(inti =0; I < list. Length; i++) {Console.Write (List[i] +" "); } Console.WriteLine (); } Public Static void USEPARAMS2(params Object[] list) { for(inti =0; I < list. Length; i++) {Console.Write (List[i] +" "); } Console.WriteLine (); }Static voidMain () {//Can pass in a comma-separated list of parameters of the specified typeUseparams (1,2,3,4); USEPARAMS2 (1,' A ',"Test");//params parameter can accept 0 or more parameters //The following function call will output a blank lineUSEPARAMS2 ();//Can pass in the parameter array, but only if the array type is the same type as the function declaration int[] Myintarray = {5,6,7,8,9}; Useparams (Myintarray);Object[] Myobjarray = {2,' B ',"Test","Again"}; USEPARAMS2 (Myobjarray);//The following function call will cause a compilation error because the passed-in parameter cannot be resolved to an integer array //useparams (myobjarray); //The following function call will not be faulted, but the integer array will become the first element of the Paras object array //This needs to be carefully understood by everyoneUSEPARAMS2 (Myintarray); }}/*output:1 2 3 4 1 a Test 5 6 7 8 9 2 B test again system.int32[]*/
[c#-4] params keyword