[c#-4] params keyword

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.