Encapsulates a method delegate that contains two parameters, without a return value.
Grammar
Public Delegate void action< in t2> ( T1 arg1, T2 arg2)
Type parameter
In T1: The first parameter type of the delegate encapsulation method, this type parameter is contravariant.
Usage
You can use ACTION<T1, t2> delegates to pass methods as arguments, instead of customizing delegates. The encapsulated method must be consistent with the method signature of this delegate. In other words, the encapsulated method also has two parameters and no return value.
A delegate named Concatstrings is explicitly declared below. It then assigns a reference to any of the two methods to its delegate instance. One of the methods writes two strings to the console, and the other writes two strings to a file.
usingSystem;usingSystem.IO;Delegate voidConcatstrings (stringString1,stringstring2); Public classtestdelegate{ Public Static voidMain () {stringMessage1 ="The first line of a message."; stringMessage2 ="The second line of a message."; Concatstrings concat; if(Environment.getcommandlineargs (). Length >1) Concat=WriteToFile; Elseconcat=Writetoconsole; Concat (Message1, Message2); } Private Static voidWritetoconsole (stringString1,stringstring2) {Console.WriteLine ("{0}\n{1}", string1, string2); } Private Static voidWriteToFile (stringString1,stringstring2) {StreamWriter writer=NULL; Try{writer=NewStreamWriter (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 following action<t1, T2> delegate simplifies the above code:
usingSystem;usingSystem.IO; Public classtestaction2{ Public Static voidMain () {stringMessage1 ="The first line of a message."; stringMessage2 ="The second line of a message."; Action<string,string>concat; if(Environment.getcommandlineargs (). Length >1) Concat=WriteToFile; Elseconcat=Writetoconsole; Concat (Message1, Message2); } Private Static voidWritetoconsole (stringString1,stringstring2) {Console.WriteLine ("{0}\n{1}", string1, string2); } Private Static voidWriteToFile (stringString1,stringstring2) {StreamWriter writer=NULL; Try{writer=NewStreamWriter (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, is a predefined delegate, do not need to customize the corresponding parameters of the delegate.
can also be used with anonymous methods:
usingSystem;usingSystem.IO; Public classtestanonymousmethod{ Public Static voidMain () {stringMessage1 ="The first line of a message."; stringMessage2 ="The second line of a message."; Action<string,string>concat; if(Environment.getcommandlineargs (). Length >1) Concat=Delegate(stringS1,stringS2) {writetofile (S1, S2);}; Elseconcat=Delegate(stringS1,stringS2) {writetoconsole (S1, S2);} ; Concat (Message1, Message2); } Private Static voidWritetoconsole (stringString1,stringstring2) {Console.WriteLine ("{0}\n{1}", string1, string2); } Private Static voidWriteToFile (stringString1,stringstring2) {StreamWriter writer=NULL; Try{writer=NewStreamWriter (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:
usingSystem;usingSystem.IO; Public classtestlambdaexpression{ Public Static voidMain () {stringMessage1 ="The first line of a message."; stringMessage2 ="The second line of a message."; Action<string,string>concat; if(Environment.getcommandlineargs (). Length >1) Concat= (S1, s2) =writetofile (S1, S2); Elseconcat= (S1, s2) =writetoconsole (S1, S2); Concat (Message1, Message2); } Private Static voidWritetoconsole (stringString1,stringstring2) {Console.WriteLine ("{0}\n{1}", string1, string2); } Private Static voidWriteToFile (stringString1,stringstring2) {StreamWriter writer=NULL; Try{writer=NewStreamWriter (Environment.getcommandlineargs () [1],false); Writer. WriteLine ("{0}\n{1}", string1, string2); } Catch{Console.WriteLine ("File write operation failed ..."); } finally { if(Writer! =NULL) writer. Close (); } }}
Neither of these has the effect of simplifying the code.
ACTION<T1, T2> commissioned