top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are param arrays in C#?

0 votes
290 views
What are param arrays in C#?
posted May 12, 2017 by Pooja Bhanout

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

At times, while declaring a method, you are not sure of the number of arguments passed as a parameter. C# param arrays (or parameter arrays) come into help at such times.

The following example demonstrates this:

using System;
namespace ArrayApplication
{
   class ParamArray
   {
      public int AddElements(params int[] arr)
      {
         int sum = 0;
         foreach (int i in arr)
         {
            sum += i;
         }
         return sum;
      }
   }

   class TestClass
   {
      static void Main(string[] args)
      {
         ParamArray app = new ParamArray();
         int sum = app.AddElements(512, 720, 250, 567, 889);
         Console.WriteLine("The sum is: {0}", sum);
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following result:

The sum is: 2938
answer May 15, 2017 by Shweta Singh
...