jrliv
6/11/2017 - 2:02 PM

Example of using the params keyword to specify a method parameter that takes a variable number of arguments.

Example of using the params keyword to specify a method parameter that takes a variable number of arguments.

static void PrintTotals(params decimal[] amount)
{
  decimal total = 0;
  foreach (decimal price in amount)
  {
    total += price;
  }
  Console.WriteLine("The total amount adds up to: $" + total);
}

static void Main(string[] args)
{
  PrintTotals(1.50m, 2.18m, 0.75m);
  PrintTotals(4.25m, 8.99m, 10m, 2.05m, 3.75m);
  PrintTotals();
}

//  The total amount adds up to: $4.43
//  The total amount adds up to: $29.04
//  The total amount adds up to: $0