Use the keyword params
, which allows you to indicate that an array argument will receive a list of parameters without having to pass an array explicitly.
public void MeuMetodo(params object[] meusParametros)
{
// usar o array de parametros
}
The argument that receives the keyword params
must be the last, and must be declared together 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 that:
MetodoComListaDeInteiros(1, 2, 5, 10);
Every method that accepts a list, also accepts an array, so the same method above can also be called:
MetodoComListaDeInteiros(new int[] { 1, 2, 5, 10 });
+1 for the use of Generics!
– Andre Figueiredo