Use the param keyword in C# to get variable parameters.
Let’s look at an example of multiplying integers. We use the params keyword to accept any number of int values -
static int Multiply(params int[] b)
The above code allows us to find the numerical multiplication of one or two int values. The following calls the same function with multiple values -
int mulVal1 = Multiply(5); int mulVal2 = Multiply(5, 10);
Let’s see the complete code to understand how variable parameters work in C# -
using System; class Program { static void Main() { int mulVal1 = Multiply(5); int mulVal2 = Multiply(5, 10); Console.WriteLine(mulVal1); Console.WriteLine(mulVal2); } static int Multiply(params int[] b) { int mul =1; foreach (int a in b) { mul = mul*a; } return mul; } }
The above is the detailed content of Variable parameters (Varargs) in C#. For more information, please follow other related articles on the PHP Chinese website!