Use Params Keyword in C#

C# params. In this article, I explain how and what is the use of the params keyword in C#.

C# Params

 
A method in C# defines a number of arguments and their types in the method declaration. When a method is called, it expects the same number and types of the arguments passed by the caller code, unless there are optional parameters. If you're not familiar with optional parameters, try Named and Optional Parameters In C#.
 
What if you're not sure of the number of arguments? This is where "params" keyword is useful.
 
The "params" keyword in C# allows a method to accept a variable number of arguments. C# params works as an array of objects. By using params keyword in a method argument definition, we can pass a number of arguments. 
 
Note: There can't be anymore parameters after a params.  
 

What does "Params" do?

 
Using "Params", the arguments passed to a method are changed by the compiler to elements in a temporary array, and this array is then used to retrieve the method parameters by the caller code. What if the creator of a class does not know how many number of parameters a caller will use?
 
For example, a Student class with a method, TotalMarks that returns the sum of all marks. Each grade may have a different number of subjects and their respective marks. The 3rd grade may have 3 subjects only. The 8th grade may have 4 subjects while a 9th grade may have 5 subjects. We can use params in this case and pass 3, 4, and 5 comma separated values. 
 

Why we use "Params" in C#?

 
As I said earlier, params keyword in C# can be used to declare method that does not know the number of parameters. Parmas are also useful to write "clean code". Instead of using various overloaded methods to pass multiple values, we can simply create an array and pass it as an arugment or a comma separated list of values.
 
Let's say, we have a class, Student. It has two methods, TotalMarks and AllSubjects. Each of these methods take a parameter of type params. As you can see from the below code, params is an array. 
  1. public class Students  
  2. {  
  3.     public static int TotalMarks(params int[] list)  
  4.     {  
  5.         int total = 0;  
  6.         for (int i = 0; i < list.Length; i++)  
  7.             total += list[i];  
  8.         return total;  
  9.     }  
  10.   
  11.     public static string AllSubjects(params string[] subjects)  
  12.     {  
  13.         System.Text.StringBuilder builder = new System.Text.StringBuilder();  
  14.         for (int i = 0; i < subjects.Length; i++)  
  15.         {  
  16.             builder.Append(subjects[i]);  
  17.             builder.Append(" ");  
  18.         }  
  19.         return builder.ToString();  
  20.     }  
  21. }  
Now, to call these methods, we can create an array of objects and pass it as a params parameter. Alternatively, we can also pass a comma separated values for params. The following code snippet shows both of these examples.  
  1. // Total for 3rd grade. Pass 3 comma separate values as params  
  2. Console.WriteLine("Params with 3 parameters");  
  3. int total3 = Students.TotalMarks(8, 9, 8);  
  4. // Print result  
  5. Console.WriteLine(total3);  
  6. // Create an array of strings   
  7. string[] subs = {"English""Reading""Writing"};  
  8. // Pass array of strings as a params and print result  
  9. Console.WriteLine(Students.AllSubjects(subs));  

C# Params Code Example

 
Here is a complete code example written in C# that shows how to pass different numbers of variables in a method as a params argument and read these values back in the caller code. 
  1. using System;  
  2.   
  3. namespace ParamsSample  
  4. {  
  5.   
  6.     public class Students  
  7.     {  
  8.         public static int TotalMarks(params int[] list)  
  9.         {  
  10.             int total = 0;  
  11.             for (int i = 0; i < list.Length; i++)  
  12.                 total += list[i];  
  13.             return total;  
  14.         }  
  15.   
  16.         public static string AllSubjects(params string[] subjects)  
  17.         {  
  18.             System.Text.StringBuilder builder = new System.Text.StringBuilder();  
  19.             for (int i = 0; i < subjects.Length; i++)  
  20.             {  
  21.                 builder.Append(subjects[i]);  
  22.                 builder.Append(" ");  
  23.             }  
  24.             return builder.ToString();  
  25.         }  
  26.     }  
  27.   
  28.     class Program  
  29.     {  
  30.   
  31.         static void Main(string[] args)  
  32.         {  
  33.             // Total for 3rd grade. Pass 3 comma separate values as params  
  34.             Console.WriteLine("Params with 3 parameters");  
  35.             int total3 = Students.TotalMarks(8, 9, 8);  
  36.             // Print result  
  37.             Console.WriteLine(total3);  
  38.             // Create an array of strings   
  39.             string[] subs = {"English""Reading""Writing"};  
  40.             // Pass array of strings as a params and print result  
  41.             Console.WriteLine(Students.AllSubjects(subs));  
  42.   
  43.             // Total for 8th grade          
  44.             Console.WriteLine("Params with 4 parameters");  
  45.             int[] marks = { 24, 22, 25, 21 };  
  46.             int total4 = Students.TotalMarks(marks);  
  47.             string str4 = Students.AllSubjects("Math""English""Art""Social Science");  
  48.             Console.WriteLine(total4);  
  49.             Console.WriteLine(str4.ToString());  
  50.   
  51.             // Total for 10th grade  
  52.             Console.WriteLine("Params with 5 parameters");  
  53.             int total5 = Students.TotalMarks(92, 90, 95, 91, 98);  
  54.             string str5 = Students.AllSubjects(new string[]{ "Math""English""Art""Social Science""Gym"});  
  55.             Console.WriteLine(total5);  
  56.             Console.WriteLine(str5.ToString());  
  57.   
  58.             Console.ReadKey();  
  59.         }  
  60.           
  61.     }  
  62. }  

C# Params Output

 
The output of the above code looks like the following:
 
CSharp Params