Create Actions list with parameters and then run it

Asked

Viewed 62 times

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();
        }
    }

1 answer

5


Before replying, two comments:

  1. The delegate with return would be Func instead of Action;
  2. When you delegate a method, who 'provides' the values of the parameters is the class that holds the invocation power of the method delegate;

Assuming you understand these points (and trying to infer some floating meaning between the question title, the example code presented and something that would be usable), it would be like this:

private List<Func<int, int, int>> ListaDelegate = new List<Func<int, int, int>>();

private int MetodoExecutar(int numero1, int numero2)
{
    return numero1 + numero2;
}

private int MetodoExecutar2(int numero1, int numero2)
{
    return numero1 * numero2;
}

private void MetodoMain()
{
    ListaDelegate.Add(MetodoExecutar);
    ListaDelegate.Add(MetodoExecutar2);
    ListaDelegate.Add((num1,num2) => { return num1 * (num1 + num2); }); // Delegate anônimo
}

private void ExecutarListaDeActions()
{
    int valor1 = 10;
    int valor2 = 20;

    foreach (Func<int, int, int> acao in ListaDelegate)
        System.Diagnostics.Debug.WriteLine("Resultado: " + acao(valor1, valor2).ToString());
}

The execution of the codes involved should print this:

// Resultado: 30
// Resultado: 200
// Resultado: 300
  • 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.

  • 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 ?

  • 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.

  • 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.

  • 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]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.