1
I would like to create a list of Actions with parameters and then do a foreach in this list and exit executing the methods, each with their respective parameters.
Something more or less with the code below.
private List<Action<int, int>> ListaAction = new List<Action<int, int>>();
private int MetodoExecutar(int numero1, int numero2)
{
return numero1 + numero2;
}
private void MetodoMain()
{
int valor1 = 1;
int valor2 = 2;
ListaAction.Add(MetodoExecutar(valor1, valor2));
}
private void ExecutarListaDeActions()
{
foreach(Action acao in ListaAction)
{
acao();
}
}
I understood your solution perfectly, but my question is this, when I include my FUNC in the list, I can not include it passing the parameters at the time of inclusion in the list? Because let’s assume, I have a routine that calculates the number me the result of the number and I want to execute the function with the result of that number, at that point I will add my method with the number that resulted above, would it be possible to store this Function together with the parameters it should run ? That would be my great doubt and solution to my problem.
– Nicola Bogar
Because if this is not possible compensates more I make a List with the result of my parameters, then perform a foreach on it, and execute my main function by passing the necessary parameters, so I do not need to use any list of FUNC<>. Correct ?
– Nicola Bogar
Well, this would be a fourth subject. The title was the 1st, the example code you wrote in the question was the 2nd and what I suggested as a solution was the 3rd.
– Diego Rafael Souza
But I signaled in the code Listaaction.Add(Methodoexecutar(valor1, valor2)); that I would like to insert the ACTION, or FUNC already with its parameters for execution, I understood perfectly what signaled in their responses, I just wanted to know even if there is the possibility to save these FUNC in the list already with its parameters for execution.
– Nicola Bogar
If your problem is as you described it now, I believe the solution is not
delegates
. It would be like using a screwdriver to dig a ditch. Maybe the proper tool is recursiveness, for example. Anyway, for me it was not very clear your problem, I suggest asking another question contextualizing the problem with a [mcve]– Diego Rafael Souza