Using a Variable Number of Arguments


When you create a method, you usually know in advance the number of arguments that you will be passing to it, but this is not always the case. Sometimes you will want to create a method that can be passed an arbitrary number of arguments. For example, consider a method that finds the smallest of a set of values. Such a method might be passed as few as two values, or three, or four, and so on. In all cases, you want that method to return the smallest value. Such a method cannot be created using normal parameters. Instead, you must use a special type of parameter that stands for an arbitrary number of parameters. This is done by creating a params parameter.

The params modifier is used to declare an array parameter that will be able to receive zero or more arguments. The number of elements in the array will be equal to the number of arguments passed to the method. Your program then accesses the array to obtain the arguments.

Here is an example that uses params to create a method called minVal( ), which returns the minimum value from a set of values:

 // Demonstrate params. using System; class Min {   public int minVal(params int[] nums) {     int m;     if(nums.Length == 0) {       Console.WriteLine("Error: no arguments.");       return 0;     }     m = nums[0];     for(int i=1; i < nums.Length; i++)       if(nums[i] < m) m = nums[i];     return m;   } } class ParamsDemo {   public static void Main() {     Min ob = new Min();     int min;     int a = 10, b = 20;     // call with two values     min = ob.minVal(a, b);     Console.WriteLine("Minimum is " + min);     // call with 3 values     min = ob.minVal(a, b, -1);     Console.WriteLine("Minimum is " + min);     // call with 5 values     min = ob.minVal(18, 23, 3, 14, 25);     Console.WriteLine("Minimum is " + min);     // can call with an int array, too     int[] args = { 45, 67, 34, 9, 112, 8 };     min = ob.minVal(args);     Console.WriteLine("Minimum is " + min);   } }

The output from the program is shown here:

 Minimum is 10 Minimum is -1 Minimum is 3 Minimum is 8

Each time minVal( ) is called, the arguments are passed to it via the nums array. The length of the array equals the number of elements. Thus, you can use minVal( ) to find the minimum of any number of values.

Notice the last call to minVal( ). It is passed an array containing the values, rather than passing the values individually. This is perfectly legal. When a params parameter is created, it will accept either a variable-length list of arguments, or an array containing the arguments.

Although you can pass a params parameter any number of arguments, they all must be of a type compatible with the array type specified by the parameter. For example, calling minVal( ) like this:

 min = ob.minVal(1, 2.2); // Wrong!

is illegal because there is no automatic conversion from double (2.2) to int, which is the type of nums in minVal( ).

When using params, you need to be careful about boundary conditions because a params parameter can accept any number of arguments—even zero! For example, it is syntactically valid to call minVal( ) as shown here:

 min = ob.minVal(); // no arguments min  = ob.minVal(3); // 1 argument

This is why there is a check in minVal( ) to confirm that at least one element is in the nums array before there is an attempt to access that element. If the check were not there, then a runtime exception would result if minVal( ) were called with no arguments. (Exceptions are described in Chapter 13.) Furthermore, the code in minVal( ) was written in such a way as to permit calling minVal( ) with one argument. In that situation, the lone argument is returned.

A method can have normal parameters and a variable-length parameter. For example, in the following program, the method showArgs( ) takes one string parameter and then a params integer array:

 // Use regular parameter with a params parameter. using System; class MyClass {   public void showArgs(string msg, params int[] nums) {     Console.Write(msg + ": ");     foreach(int i in nums)       Console.Write(i + " ");     Console.WriteLine();   } } class ParamsDemo2 {   public static void Main() {     MyClass ob = new MyClass();     ob.showArgs("Here are some integers",                 1, 2, 3, 4, 5);     ob.showArgs("Here are two more",                 17, 20);   } }

This program displays the following output:

 Here are some integers: 1 2 3 4 5 Here are two more: 17 20

In cases where a method has regular parameters and a params parameter, the params parameter must be the last one in the parameter list. Furthermore, in all situations, there must be only one params parameter.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net