Action & lt; T1, T2 & gt; delegate, actiont1t2 delegate

Source: Internet
Author: User

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.

Related Article

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.