C# keywords - method parameters

Printed From Microsoft.docs;

Use the paramskeyword to specify method parameters that take a variable number of parameters .

A comma-separated list of parameters of the type specified in the parameter declaration or an array of parameters of the specified type can be sent. It is also possible not to send parameters. If no arguments are sent, the length of the paramslist is zero.

No other parameters are allowed after the paramskeyword in a method declaration, and only one paramskeyword is allowed in a method declaration.

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()
    {
        // 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 be 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 into an integer array.
        //UseParams(myObjArray);

        // The following call does not cause an error, but the entire 
        // 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__Define a function with an indeterminate number of parameters_ {
     class Program {
         static  int Sum( int [] array) // If a function defines parameters, it must be passed when calling this function The parameter of the corresponding type, otherwise it cannot be called (the compiler fails to compile) 
        {
             int sum = 0 ;
             for ( int i = 0 ; i < array.Length; i++ )
            {
                sum += array[i];
            }
            return sum;
        }

        static  int Plus( params  int [] array) // A parameter array of int type is defined here. The difference between the parameter array and the array parameter (above) lies in the function call. When calling the function of the parameter array, we can pass Come over with any number of parameters, and then the compiler will automatically form an array for us. If the parameter is the above array parameter, then we will manually create the array by ourselves 
        {
             int sum = 0 ;
             for ( int i = 0 ; i < array.Length; i++ ) {
                sum += array[i];
            }
            return sum;
        }
        static void Main(string[] args)
        {
            int sum = Sum(new int[] {23, 4, 34, 32, 32, 42, 4});
            Console.WriteLine(sum);
            int sum2 = Plus( 23 , 4 , 5 , 5 , 5 , 32 , 423 , 42 , 43 , 23 , 42 , 3 ); // The parameter array is to help us reduce the process of creating an array 
            Console.WriteLine(sum2) ;
            Console.ReadKey();
        }
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325208093&siteId=291194637