Action <T1, T2> delegate, actiont1t2 delegate
Encapsulate a method delegate that contains two parameters without returning values.
Syntax
public delegate void Action<in T1, in T2>( T1 arg1, T2 arg2)
Type parameter
In T1: the first parameter type of the delegate encapsulation method. This parameter type is inverter.
Usage
You can use the Action <T1, T2> delegate to pass the method in the form of parameters, instead of using a custom delegate. The encapsulated method must be the same as the signature of the delegate method. That is to say, the encapsulated method also has two parameters with no return value.
The following explicitly declares a delegate named ConcatStrings. It then assigns any reference of the two methods to its delegated instance. One method writes two strings to the console, and the other two strings to the file.
using System;using System.IO;delegate void ConcatStrings(string string1, string string2);public class TestDelegate{ public static void Main() { string message1 = "The first line of a message."; string message2 = "The second line of a message."; ConcatStrings concat; if (Environment.GetCommandLineArgs().Length > 1) concat = WriteToFile; else concat = WriteToConsole; concat(message1, message2); } private static void WriteToConsole(string string1, string string2) { Console.WriteLine("{0}\n{1}", string1, string2); } private static void WriteToFile(string string1, string string2) { StreamWriter writer = null; try { writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false); writer.WriteLine("{0}\n{1}", string1, string2); } catch { Console.WriteLine("File write operation failed..."); } finally { if (writer != null) writer.Close(); } }}
The preceding code is simplified with the Action <T1, T2> delegate:
using System;using System.IO;public class TestAction2{ public static void Main() { string message1 = "The first line of a message."; string message2 = "The second line of a message."; Action<string, string> concat; if (Environment.GetCommandLineArgs().Length > 1) concat = WriteToFile; else concat = WriteToConsole; concat(message1, message2); } private static void WriteToConsole(string string1, string string2) { Console.WriteLine("{0}\n{1}", string1, string2); } private static void WriteToFile(string string1, string string2) { StreamWriter writer = null; try { writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false); writer.WriteLine("{0}\n{1}", string1, string2); } catch { Console.WriteLine("File write operation failed..."); } finally { if (writer != null) writer.Close(); } }}
In fact, it is a pre-defined delegate and does not need to customize the corresponding parameter delegate.
It can also be used together with the anonymous method:
using System;using System.IO;public class TestAnonymousMethod{ public static void Main() { string message1 = "The first line of a message."; string message2 = "The second line of a message."; Action<string, string> concat; if (Environment.GetCommandLineArgs().Length > 1) concat = delegate(string s1, string s2) { WriteToFile(s1, s2); }; else concat = delegate(string s1, string s2) { WriteToConsole(s1, s2);} ; concat(message1, message2); } private static void WriteToConsole(string string1, string string2) { Console.WriteLine("{0}\n{1}", string1, string2); } private static void WriteToFile(string string1, string string2) { StreamWriter writer = null; try { writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false); writer.WriteLine("{0}\n{1}", string1, string2); } catch { Console.WriteLine("File write operation failed..."); } finally { if (writer != null) writer.Close(); } }}
You can also replace an anonymous function with a lambda expression:
using System;using System.IO;public class TestLambdaExpression{ public static void Main() { string message1 = "The first line of a message."; string message2 = "The second line of a message."; Action<string, string> concat; if (Environment.GetCommandLineArgs().Length > 1) concat = (s1, s2) => WriteToFile(s1, s2); else concat = (s1, s2) => WriteToConsole(s1, s2); concat(message1, message2); } private static void WriteToConsole(string string1, string string2) { Console.WriteLine("{0}\n{1}", string1, string2); } private static void WriteToFile(string string1, string string2) { StreamWriter writer = null; try { writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false); writer.WriteLine("{0}\n{1}", string1, string2); } catch { Console.WriteLine("File write operation failed..."); } finally { if (writer != null) writer.Close(); } }}
Both of them do not simplify the code.