Dùng từ khóa params cho variable arguments.
Ví dụ nhân các số int:
static int Multiply(params int[] b)
Gọi với 1 hoặc 2 tham số:
int mulVal1 = Multiply(5); int mulVal2 = Multiply(5, 10);
Ví dụ
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;
}
}