8.11 Variable-Length Argument Lists

Variable-length argument lists allow you to create methods that receive an arbitrary number of arguments. A one-dimensional array-type argument preceded by the keyword params in a method’s parameter list indicates that the method receives a variable number of arguments with the type of the array’s elements. This use of a params modifier can occur only in the parameter list’s last parameter. While you can use method overloading and array passing to accomplish much of what is accomplished with variable-length argument lists, using the params modifier is more concise.

Figure 8.23 demonstrates method Average (lines 8–19), which receives a variable-length sequence of doubles (line 8). C# treats the variable-length argument list as a one-dimensional array whose elements are all of the same type. Hence, the method body can manipulate the parameter numbers as an array of doubles. Lines 13–16 use the foreach loop to walk through the array and calculate the total of the doubles in the array. Line 18 accesses numbers.Length to obtain the size of the numbers array for use in the averaging calculation. Lines 30, 32 and 34 in Main call method Average with two, three and four arguments, respectively. Method Average has a variable-length argument list, so it can average as many double arguments as the caller passes. The output reveals that each call to method Average returns the correct value.

Common Programming Error 8.7

The params modifier may be used only with the last parameter of the parameter list.

Fig. 8.23 Using variable-length argument lists.

Alternate View

  1   // Fig. 8.23: ParamArrayTest.cs
  2   // Using variable-length argument lists.
  3   using System;
  4
  5   class ParamArrayTest
  6   {
  7      // calculate average
  8      static double Average(params double[] numbers)
  9      {
 10         var total = 0.0; // initialize total
 11
 12         // calculate total using the foreach statement
 13         foreach (var d in numbers)
 14         {
 15            total += d;
 16         }
 17
 18         return numbers.Length != 0 ? total / numbers.Length : 0.0;
 19      }
 20
 21      static void Main()
 22      {
 23         var d1 = 10.0;
 24         var d2 = 20.0;
 25         var d3 = 30.0;
 26         var d4 = 40.0;
 27
 28         Console.WriteLine(
 29            $"d1 = {d1:F1}
d2 = {d2:F1}
d3 = {d3:F1}
d4 = {d4:F1}
");
 30         Console.WriteLine($"Average of d1 and d2 is {Average(d1, d2):F1}");
 31         Console.WriteLine(
 32            $"Average of d1, d2 and d3 is {Average(d1, d2, d3):F1}");
 33         Console.WriteLine(
 34            $"Average of d1, d2, d3 and d4 is {Average(d1, d2, d3, d4):F1}");
 35      }
 36   }

d1 = 10.0
d2 = 20.0
d3 = 30.0
d4 = 40.0

Average of d1 and d2 is 15.0
Average of d1, d2 and d3 is 20.0
Average of d1, d2, d3 and d4 is 25.0
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset