C # Keywords-method parameters

Source: Internet
Author: User

Tag: Otherwise new + + SPL lang ref you more method parameter

Printed from Microsoft.docs;

Use params keywords to specify method parameters that take a variable number of parameters.

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 length of the list is zero.

No additional parameters are allowed after the keyword in the method declaration params , and only one keyword is allowed in the method declaration params .

 Public classmyclass{ Public Static voidUseparams (params int[] list) {         for(inti =0; I < list. Length; i++) {Console.Write (List[i]+" ");    } Console.WriteLine (); }     Public Static voidUSEPARAMS2 (params Object[] list) {         for(inti =0; I < list. Length; i++) {Console.Write (List[i]+" ");    } Console.WriteLine (); }    Static voidMain () {//You can send a comma-separated list of arguments of the//specified type.Useparams (1,2,3,4); USEPARAMS2 (1,'a',"Test"); //A params parameter accepts zero or more arguments. //The following calling statement displays only a blank line.UseParams2 (); //An array argument can passed, as long as the array//type matches the parameter type of the method being called.        int[] Myintarray = {5,6,7,8,9 };        Useparams (Myintarray); Object[] Myobjarray = {2,'b',"Test","again" };        USEPARAMS2 (Myobjarray); //The following call causes a compiler error because the object//array cannot be converted to an integer array. //Useparams (Myobjarray); //The following call does not cause a error, but the entire//The integer array becomes the first element of the params array.UseParams2 (Myintarray); }}/*output:1 2 3 4 1 a Test 5 6 7 8 9 2 B test again system.int32[]*/

namespace_050_ parameter array __ defines a function with an indeterminate number of parameters _ {classProgram {Static intSum (int[] Array)//If a function defines a parameter, it must be passed the corresponding type parameter when calling the function, otherwise it cannot be called (compiler compilation does not pass)        {            intsum =0;  for(inti =0; I < array. Length; i++) {sum+=Array[i]; }            returnsum; }        Static intPlus (params int[] Array)//here defines an int type of parameter array, parameter arrays and array parameters (above) different, in the function call, call the function of the parameter array, we can pass over any number of arguments, and then the compiler will help us to automatically set up an array, parameters if the above array parameters, So this array we ourselves go to create manually        {            intsum =0;  for(inti =0; I < array. Length; i++) {sum+=Array[i]; }            returnsum; }        Static voidMain (string[] args) {            intsum = SUM (New int[] { at,4, the, +, +, the,4});            Console.WriteLine (sum); intsum2 = Plus ( at,4,5,5,5, +,423, the, +, at, the,3);//A parameter array is a way to reduce the process of creating an array .Console.WriteLine (sum2);        Console.readkey (); }    }}

C # Keywords-method parameters

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.