How to pass any number of parameters into a method in C# .NET
December 15, 2016 Leave a comment
You must have come across built-in methods in .NET where you can send any number of arguments into a method. E.g. string.Format has an overload where you can pass in a format string and then an array with the “params” modifier.
There’s nothing stopping you from using the same keyword to write a similar method, here’s an example:
public void MethodWithParams(int toBeMultiplied, params int[] multipliers)
{
foreach (int m in multipliers)
{
Console.WriteLine(string.Format("{0} x {1} = {2}", toBeMultiplied, m, toBeMultiplied * m));
}
}