C#中的参数数组

在本文中,我将解释如何创建参数数组。

我们可以通过关键字"params"在函数参数中定义参数数组,后跟<type name>,然后数组名称。
add函数将允许多个参数并执行添加操作。

namespace demo_array

{
    class Program

    {
        static int Add(params int[] nums)
        {
            int total = 0;

            foreach (int i in nums)
            {
                total = total + i;
            }

            return total;
        }
        static void Main(string[] args)
        {

            Console.WriteLine("Parameter Array Function Testing ...");

            int result = 0;

            /* function allowing 3 arguments */
            result = Add(10, 15, 20);
            Console.WriteLine("Result for 3 Prameter :{0}", result);
            Console.ReadKey();

        }

    }
}
日期:2020-04-11 23:03:46 来源:oir作者:oir