国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Action<T1, T2>委托

2019-11-17 02:31:29
字體:
供稿:網(wǎng)友

Action<T1, T2>委托

封裝包含兩個參數(shù)的方法委托,沒有返回值。

語法

public delegate void Action<in T1, in T2>(    T1 arg1,    T2 arg2)

類型參數(shù)

in T1:委托封裝方法的第一個參數(shù)類型,此類型參數(shù)逆變。

用法

可以使用Action<T1, T2>委托以參數(shù)形式傳遞方法,而不用自定義委托。封裝的方法必須與此委托的方法簽名一致。也就是說,封裝的方法也要有兩個參數(shù),沒有返回值。

下面顯式聲明了一個名為ConcatStrings的委托。然后,它將兩個方法中的任意一個的引用分配給其委托實例。其中一個方法將兩個字符串寫入控制臺;另一個將兩個字符串寫入文件。

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();      }         }}

下面以Action<T1, T2>委托簡化上面的代碼:

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();      }         }}

其實就是預(yù)先定義好的委托,不需要自定義對應(yīng)參數(shù)的委托了。

還可以同匿名方法一起使用:

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();      }         }}

也可以將匿名函數(shù)替換為lambda表達式:

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();      }         }}

后兩者都沒有起到簡化代碼的作用。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 五峰| 渝中区| 靖边县| 金门县| 墨竹工卡县| 湘潭县| 获嘉县| 奈曼旗| 武冈市| 宁南县| 亚东县| 金门县| 北辰区| 唐山市| 山阴县| 玉屏| 高青县| 准格尔旗| 朔州市| 上思县| 新晃| 长岭县| 龙川县| 大田县| 错那县| 阳春市| 綦江县| 阿坝县| 建阳市| 上虞市| 公安县| 如皋市| 延吉市| 沙河市| 梅州市| 任丘市| 高平市| 巴林右旗| 河源市| 防城港市| 新津县|