国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 洛扎县| 达尔| 塔城市| 塔城市| 宜阳县| 汤原县| 鄂州市| 郎溪县| 盐池县| 贵港市| 崇信县| 双鸭山市| 棋牌| 囊谦县| 比如县| 若尔盖县| 奉新县| 宁国市| 丰顺县| 涞源县| 丰宁| 丽水市| 拉孜县| 德阳市| 澎湖县| 农安县| 临泉县| 长治市| 顺义区| 甘孜县| 小金县| 和龙市| 凤阳县| 朔州市| 瑞金市| 榕江县| 久治县| 吉首市| 宜兰市| 阿拉尔市| 西安市|