By using the params
keyword, you can specify a method parameter that takes a variable number of arguments. No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration. The declared type of the params parameter must be a single-dimensional array.
public static void UseParams(params int[] list) {
for (int i = 0; i < list.Length; i++) {
Console.Write(list[i] + " ");
}
Console.WriteLine();
}
public static void UseParams2(params object[] list) {
for (int i = 0; i < list.Length; i++) {
Console.Write(list[i] + " ");
}
Console.WriteLine();
}
// usage
UseParams(1, 2, 3, 4);
UseParams2(1, 'a', "test");