ACTION<T1, T2> commissioned

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.