Defining Functions with a Variable Number of Parameters


The C# language also has a way to pass a variable number of parameters to a function. (A variable number of parameters could be zero parameters as well).

To add a function that accepts a variable number of parameters:

  1. Add a function with any return type.

  2. In the parameter list type param int[] args where int[] is an array of any type, and args is any variable to hold the arguments ( Figure 6.6 ).

    Figure 6.6 It's a well-known fact that Santa uses C# to make up his list, and because there is a variable number of names each year that are part of the Nice list he needs to use a "params" parameter.
     class Santa {    public void AddToNiceList(  params string[] names  )    {    } } 
  3. Retrieve the parameters using the same mechanisms that you would use when retrieving values from an array ( Figure 6.7 ).

    Figure 6.7 When Santa is done adding names to the list he can treat the names as an array. One way to navigate through all the names of the array is to use the foreach function.
     class Santa {    public void AddToNiceList(                params string[] names)    {  foreach( string onename in names)   {   WriteToNamesDB(onename);   }  } } 

graphics/tick.gif Tips

  • You can call a function that has a params argument in one of two ways. You could call the function passing a number of arguments of the same type separated by commas ( Figure 6.8 ), or you could call the function passing a single array of the type of the function parameter ( Figure 6.9 ).

    Figure 6.8 When working with params parameters you can call the function passing any number of values, or no values. The compiler creates an array from the values and calls the function.
     class Santa {    public void AddToNiceList(                   params string[] names)    {    } } class Christmas {    void PrepareLists()    {      Santa Nick1 = new Santa();      Nick1.AddToNiceList(  "Tom", "Sue", "Jane", "Bill"  );    } } 
    Figure 6.9 Another way to call a function that has a params parameter is to take matters in your own hands and create an array yourself, then pass the array as a single parameter.
     class Santa {    public void AddToNiceList(                   params string[] names)    {    } } class Christmas {    void PrepareLists()    {      Santa Nick1 = new Santa();  string[] names =   { "Tom", "Sue", "Jane" ,"Bill"};  Nick1.AddToNiceList(  names  );    } } 
  • If you pass a variable number of parameters to the function, the C# compiler invisibly builds an array with all the parameters and passes the array to the function.

  • You can add multiple constructors to the class, but only one is triggered when the developer creates an instance of the class. The way the compiler knows which constructor to invoke is based on how the developer uses the new operator. For example, if the class has a default constructor and a constructor that accepts one integer parameter, then when the developer writes Account acct = new Account() , the compiler will invoke the default constructor; and when the developer writes Account acct = new Account(100) , the compiler will invoke the constructor that accepts one parameter.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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