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