Home > Article > Backend Development > How to pass parameters using param array in C# method?
When declaring a method, you are not sure of the number of arguments passed as formal parameters. This is where the C# parameter array (or parameter array) comes in handy.
This is how to use parameters -
public int AddElements(params int[] arr) { }
Here is the complete example -
using System; namespace Program { class ParamArray { public int AddElements(params int[] arr) { int sum = 0; foreach (int i in arr) { sum += i; } return sum; } } class Demo { static void Main(string[] args) { ParamArray app = new ParamArray(); int sum = app.AddElements(300, 250, 350, 600, 120); Console.WriteLine("The sum is: {0}", sum); Console.ReadKey(); } } }
The sum is: 1620
The above is the detailed content of How to pass parameters using param array in C# method?. For more information, please follow other related articles on the PHP Chinese website!