0

I'm trying to figure out how to properly use an Action delegate that takes more than one (through ten) type arguments like so:

Action<T,T,T> for example.

Let's say I'm using the following function prototype:

public void SomeFunc(Action<bool, int> foo)

How would I go about passing a boolean and an integer value to foo when calling that function? Maybe I'm just getting this entire thing wrong, but when looking it up on MSDN and other sources I just can't get my head around how this thing actually works. Same for its Func<TReturn, T, T> counterpart.

MSDN comes up with some pretty incomprehensible examples, and I'm hoping someone here can actually show me an example of how these delegates are implemented properly. Thanks.

aevitas
  • 3,753
  • 2
  • 28
  • 39
  • With `Func`, the order is the other way around: [`Func`](http://msdn.microsoft.com/en-us/library/bb534647). – svick Aug 31 '12 at 01:24

4 Answers4

4

You can use a lambda like this:

SomeFunc( (a1, a2) => {
    Console.WriteLine(string.Format("params: {0}, {1}", a1, a2));
});

Or, if you already have a function that matches the delegate, you can pass it directly.

void SomeFunc(Action<bool,int> foo)
{
    foo(true, 99);
    // stuff
}

void matchesDelegate(bool a1, int a2)
{
    Console.WriteLine(string.Format("params: {0}, {1}", a1, a2));
}

SomeFunc(matchesDelegate);

Outputs:

params: true, 99

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
2

With a signature like:

public void SomeFunc(Action<bool, int> foo)

You call SomeFunc and pass in only one parameter - an action that takes two parameters. Think of the action as a callback method as that is what it really is. Somewhere in the SomeFunc method it will call foo and pass in the parameters there. You don't need to supply them yourself.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
2

Action and Func delegates work just like any other. The simple Action delegate looks basically like:

public delegate void Action();

and is called like so:

public void doAction(Action foo)
{
    Action(); // You call the action here.
}

Using generic arguments, the definition basically looks like:

public delegate void Action(T1, T2, T3);

and is called like the following to pass parameters into the Action:

public void SomeFunc(Action<bool,int> foo)
{
    bool param1 = true;
    int param2 = 69;
    foo(param1, param2); // Here you are calling the foo delegate.
}

Just like other delegates, these delegates can be assigned with explicitly defined functions,

public void AnAction()
{
    Console.WriteLine("Hello World!");
}

doAction(AnAction);

or with anonymous functions.

Action<bool,int> anonymousAction = (b,i) =>
{
    if (b == true && i > 5)
        Console.WriteLine("Hello!");
    else
        Console.WriteLine("Goodbye!");
}
SomeFunc(anonymousAction);
Tanzelax
  • 5,406
  • 2
  • 25
  • 28
1

If foo is Action<bool, t> you can call it with foo(true, 42)

If you want to call SomeFunc, you can call it with a method:

SomeFunc(MyMethod);

where you have:

public void MyMethod(bool b, int i){}

Or you can call it with an anonymous method:

SomeFunc(delegate(bool b, int i) {Trace.WriteLine(b); Trace.WriteLine(i);});

Or you can call it with a lambda:

someFunc((b,i) => {Trace.WriteLine(b); Trace.WriteLine(i);});
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98