First off, there is a lot of info out there: a simple google could have got you relevenat info very quickly:
MSDN[
^]
You use an Action<T> delegate to pass a method as a parameter to another method without having to declare a formal delegate. The method you pass can return no value, and takes a single parameter of type "T".
This is taken direct from MSDN:
delegate void DisplayMessage(string message);
public static void Main()
{
DisplayMessage messageTarget;
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine;
messageTarget("Hello, World!");
}
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
Can be simplified to:
public static void Main()
{
Action<string> messageTarget;
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine;
messageTarget("Hello, World!");
}
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
</string>
Both examples do the same job: you can specify that output goes to a message box (via ShowWindowsMessage) or to the console once in advance, and never have to check again.
The second example just saves you having to declare a delegate to define what method signatures can be used.