Parameters Array in C#

In this article I will explain that how to create Parameters Array.
  • 3555

We can define param array in Function argument by keyword "params" followed by <type name> and then array name. add function that will allow as many arguments and perform the addition operation.

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();

        }

 

    }

}

Output

parameter array.jpg

Further Readings
 
You may also want to read these related articles.

Ask Your Question 

Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.