12

I have a method which accepts an Action delegate and executes the given method as shown here:

public void ExpMethod(Action inputDel)
{
    inpuDel();
}

I can call above given method like this:

ExpMethod(() => {/*do something that matters*/});

Everything works fine. So far so good. Now I want to have a method which takes a generic Action delegate as an input parameter - like this:

public void ExpGenMethod(Action<string,int> inputDel)
{
    // I don't know how to call the supplied delegate as it requires parameters
}

Also, I am trying to call this ExpGenMethod in this way:

ExpGenMethod(("Hi",1) => {/*do something that makes sense*/});

But it shows syntax errors. Please let me know how to use generic action delegate in this case?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
gliese 581 g
  • 349
  • 1
  • 4
  • 17

4 Answers4

19

The whole point of a delegate is to have a pointer to a method. Passing parameters to it while it´s being declared is therefor pointless. Instead pass the arguments for your delegate within the method that executes the delegate, in your case within ExpGenMethod:

You should do this instead:

public void ExpGenMethod(Action<string,int> inputDel)
{
    inputDel("Hi", 1);
}

And call it like this:

ExpGenMethod((x, y) => {/*do something that makes sense*/});

When executing that delegate x evaluates to "Hi" and y to 1.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Are you expecting the parameters to be `"Hi", 1` always ? – Vinod Srivastav Mar 20 '19 at 18:47
  • @VinodSrivastav Of course not. Those values shpuld be computed by whatever code - either within that method or via args passed to it. The point of the answer just was the param-values are not **part of** the delegate. Instead they are **passed to** it. – MakePeaceGreatAgain Mar 20 '19 at 20:34
6

Typically, you'll want the heavy lifting to happen in the ExpGenMethod and in the delegate itself you'll simply be passing the parameters to the ExpGenMethod.

using System;

public class Program
{
    public static void Main()
    {
        ExpGenMethod((options) =>
        {
            options.x = "Hi";
            options.y = 1;
        });
    }

    public static void ExpGenMethod(Action<Options> inputDel)
    {
        var options = new Options();
        inputDel(options);
        /* have access to x and y so do some thing useful with these values */
        Console.WriteLine(options.x);
        Console.WriteLine(options.y);
    }
}

public class Options
{
    public string x { set; get;}

    public int y { set; get; }
}

Sangeet Agarwal
  • 1,674
  • 16
  • 25
  • 1
    Well appreciated answer, @Sangeet you save my time. – Biju Kalanjoor Apr 13 '22 at 08:20
  • Thanks. Could you please tell me what is the advantage of abstracting away the x and y arguments into an Options class, and then passing said Options instance using a Lambda function? – crazyTech Aug 30 '22 at 22:58
5

(a,b) => {/*do something that matters*/} means that a and b are parameters which are going to be specified during the call. Here you are using constant so you should do something like () => { use "Hi"; use 1;} and that would get you back to your first working example.

If you want to pass parameter you cna do it this way:

public void work()
{
    ExpGenMethod((a) => {/*do something that matters*/});
}

public void ExpGenMethod(Action<int> inputDel, int parameterToUse)
{
    inputDel(parameterToUse);
}
Bruno Belmondo
  • 2,299
  • 8
  • 18
0

As a follow on to what @HimBromBeere explained:

The keyword Action is defined as delegate:

public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2);

So if the method is defined as:

public void ExpGenMethod(Action<string,int> inputDel)
{
    inputDel("Hi", 1);
}

You can call ExpGenMethod with parameters x,y is using a Lambda expression, and see the results using Console.Writeline as follows:

ExpGenMethod((x, y) => { Console.WriteLine($"{x} {y}"); });
Orin
  • 351
  • 5
  • 13