Use the keyword params
, which allows you to indicate that an array argument will receive a parameter list without explicitly passing an array.
public void MeuMetodo(params object[] meusParametros)
{
// usar o array de parametros
}
The argument that receives the keyword params
should be the last one, and should be declared along with an array type.
To pass a list of integers, for example:
public void MetodoComListaDeInteiros(params int[] arrayInteiros)
{
// faça algo com os inteiros... e.g. iterar eles num laço for, e mostrar na tela
foreach (var eachNum in arrayInteiros)
Console.WriteLine(eachNum);
}
and then call it like this:
MetodoComListaDeInteiros(1, 2, 5, 10);
Every method that accepts a list also accepts an array, so the same method above can also be called like this:
MetodoComListaDeInteiros(new int[] { 1, 2, 5, 10 });