C# | Params
Params is an important keyword in C#. It is used as a parameter which can take the variable number of arguments.
Important Point About Params Keyword :
- It is useful when programmer don’t have any prior knowledge about the number of parameters to be used.
- Only one Params keyword is allowed and no additional Params will be allowed in function declaration after a params keyword.
- The length of params will be zero if no arguments will be passed.
Examples: To illustrate the use of params keyword
- Simple program to show the params keyword usage
// C# program to illustrate the// use of params keywordusingSystem;namespaceExamples {classGeeks {// function containing params parameterspublicstaticintAdd(paramsint[] ListNumbers){inttotal = 0;// foreach loopforeach(intiinListNumbers){total += i;}returntotal;}// Driver CodestaticvoidMain(string[] args){// Calling function by passing 5// arguments as followsinty = Add(12,13,10,15,56);// Displaying resultConsole.WriteLine(y);}}}Output :
106
Explanation : There is no need to define the size of the array because using params keyword in above program, the Integer data will be in the form:
ListNumbers
[0] 12
[1] 13
[2] 10
[3] 15
[4] 56 - Object type Params will allow any type of arguments and any number of arguments as follows :
// C# program to illustrate the// use of object type paramsusingSystem;namespaceExample2 {classGeeks {// function using object type paramspublicvoidresult(paramsobject[] array){for(inti = 0; i < array.Length; i++){// Display resultConsole.WriteLine(array[i]);}}// Driver CodestaticvoidMain(string[] args){Geeks gfg =newGeeks();// Variable length argumentsgfg.result("Geeks","GFG","ProGeek Cup 2.0","G4G","100");}}}Output :
Geeks GFG ProGeek Cup 2.0 G4G 100
Explanation : In above program the object type params parameter can accept any type of data and any number of arguments.


.png)
