Params In C#: Pass Variable Number Of Parameters To Method


In this tip we will discuss about params keyword in C# and how to pass variable number of parameters to method.


Suppose their is a method GetConcat (string name) which takes one parameter of sting type. And method returns merge string of some hard-code text, separator and name (name of a person). It will work fine when we will pass two parameters to this method. But if a user want to pass one more parameter (name of a person) like GetConcat (string name, string name2), then this method will not work perfectly. Again another user want to pass 5 params (name of persons), then it would be difficult to write different methods for different users.


C# provides an elegant solution to resolve preceding problem by using params keyword. Using params we can pass variable number of parameters to a method.

There are some restrictions using the params keyword:

  • You can only use the params keyword for one parameter in your method declaration.
  • Params must be always the last parameter in method

Using Code

In the following code, GetConcat() is taking two params, one is separator and another is string array with params.

  1. public string GetConcat(params string[] names)    
  2. {    
  3.       string result = "";    
  4.       if (names.Length > 0)    
  5.       {            
  6.         result += names[0];    
  7.       }    
  8.       for (int i = 1; i < names.Length; i++)    
  9.       {            
  10.             result = ", " + names[i];    
  11.       }    
  12.       return result;    
  13. }   
Call the method:
  1. string tempResult = GetConcat("Ajay""Bijay""Sanjay");  
Here we are passing 4 parameters for names. Run-time compiler creates a temp array and holds all names, after that pass GetConcat().


Ajay, Bijay, Sanjay

We can resolve the above problems without using params, but how? We can declare an array of string type and pass that variable to your method. Here is the code:
  1. public string GetConcat(string[] names)    
  2. {    
  3.       // Your logic    
  4. }    
  6. string[] names = { "Ajay""Bijay""Sanjay" };    
  7. string tempResult = GetConcat(names);    
You will get same result here. Again another question here is why we will use params. Reason is in params we didn't create any array, we pass arguments as comma separated. But here we need to define an array then pass that variable to method.

Suppose you don't know about type of data you are passing to method, then you can use object[]. Here is the code:
  1. public static string GetConcat(params object[] names)    
  2. {    
  3.     string result = "";    
  4.     if (names.Length > 0)    
  5.     {    
  6.         result += names[0];    
  7.     }    
  8.     for (int i = 1; i < names.Length; i++)    
  9.     {    
  10.         result = result + ", " + names[i];    
  11.     }    
  12.     return result;    
  13. }    
The best example of params in method are:



In this tip, we discussed how to pass variable number of parameters to a method. Hope it helps you.

Up Next
    Ebook Download
    View all
    View all