7.8 Passing variable numbers of parameters into C# methodsThis is a useful feature in C# which is not found in Java. Using the C# params keyword, a method can take in a variable number of parameters (which may not be known until runtime). The params keyword will be useful for writing methods in shared codes, in which the programmer wants to provide maximum convenience and flexibility for users. The parameters are taken in as an array (you can take in an array of object types [9] “ which means it can accept anything since every C# object is a subclass of object ).
This is how you declare a method called Test1 which takes in an arbritrary number of int parameters: public static void Test1( params int[] list) You can invoke Test1 like this: Test1(9, 23, 34); in which case the local variable, list will be initialized to an int array of size three with list[0] , list[1], and list[2] set to 9 , 23 , and 34 respectively. You can also invoke Test1 like this: Test1(9); in which case the local variable list will be initialized to an int array of size one with list[0] set to 9 only. Let's consider the example of another method called Test3 which is declared as follows : public static void Test3(string temp, params double[] list) Test3 takes in a string followed by an arbitrary number of double values. You can invoke Test3 like this: Test3("Hello!", 3.4, 4.5); in which case the local variable temp will be set to Hello , and list will be a double array of size 2 with list[0] and list[1] set to 3.4 and 4.5 , respectively. Examine the full example below showing how params is used. It should be self-explanatory. 1: using System; 2: 3: public class TestClass{ 4: 5: public static void Test1( params int[] list){ 6: Console.WriteLine("running Test1 ----------"); 7: for(int i=0; i<list.Length; i++) 8: Console.WriteLine(i + ": " + list[i]); 9: } 10: 11: public static void Test2( params object[] list){ 12: Console.WriteLine("running Test2 ----------"); 13: for(int i=0 ;i<list.Length ;i++) 14: Console.WriteLine(i + ": " + (object)list[i]); 15: } 16: 17: public static void Test3(string temp, params double[] list){ 18: Console.WriteLine("running Test3 ----------"); 19: Console.WriteLine("temp: " + temp); 20: for(int i=0 ;i<list.Length ;i++) 21: Console.WriteLine(i ++ ": " + list[i]); 22: } 23: 24: public static void Main(){ 25: 26: // passing in variable number of parameters 27: Test1(1, 2, 3, 4, 5); 28: Test2("hot", 99, 'c', "weather"); 29: Test3("apple", 4.5, 1.2, 8.88, 9.001); 30: 31: // passing in an array 32: int [] array = {3, 6, 9, 12}; 33: Test1(array); 34: } 35: } Output: c:\expt>test running Test1 ---------- 0: 1 1: 2 2: 3 3: 4 4: 5 running Test2 ---------- 0: hot 1: 99 2: c 3: weather running Test3 ---------- temp: apple 0: 4.5 1: 1.2 2: 8.88 3: 9.001 running Test1 ---------- 0: 3 1: 6 2: 9 3: 12 Not only can you pass a variable number of parameters when invoking a method which takes in a params parameter, you can also pass over an array object. The array's type must match the params 's type. On line 33 of the example above, an array object is passed over to method Test1 Additional notes
|