博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Action<T1, T2>委托
阅读量:4551 次
发布时间:2019-06-08

本文共 4554 字,大约阅读时间需要 15 分钟。

封装包含两个参数的方法委托,没有返回值。

语法

public delegate void Action
( T1 arg1, T2 arg2)

类型参数

in T1:委托封装方法的第一个参数类型,此类型参数逆变。

用法

可以使用Action<T1, T2>委托以参数形式传递方法,而不用自定义委托。封装的方法必须与此委托的方法签名一致。也就是说,封装的方法也要有两个参数,没有返回值。

下面显式声明了一个名为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
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(); } }}

其实就是预先定义好的委托,不需要自定义对应参数的委托了。

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

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

也可以将匿名函数替换为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
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(); } }}

后两者都没有起到简化代码的作用。

转载于:https://www.cnblogs.com/jiawei-whu/p/4341316.html

你可能感兴趣的文章
微信小程序picker组件 - 省市二级联动
查看>>
Dynamics CRM 给视图配置安全角色
查看>>
Eclipse修改已存在的SVN地址
查看>>
C++ ACM基础
查看>>
(转)使用 python Matplotlib 库绘图
查看>>
进程/线程切换原则
查看>>
正则表达式语法
查看>>
20165301 2017-2018-2 《Java程序设计》第四周学习总结
查看>>
Vue的简单入门
查看>>
urllib 中的异常处理
查看>>
【SQL Server高可用性】高可用性概述
查看>>
通过SQL Server的扩展事件来跟踪SQL语句在运行时,时间都消耗到哪儿了?
查看>>
SQL优化:重新编译存储过程和表
查看>>
PCB“有铅”工艺将何去何从?
查看>>
Solr环境搭建
查看>>
垂直居中的几种实现方法
查看>>
UILabel标签文字过长时的显示方式
查看>>
H5离线缓存机制-manifest
查看>>
比较:I/O成员函数getline() 与 get()(第二种用法)的用法异同
查看>>
201671010118 2016-2017-2《Java程序设计》 第十一周学习心得
查看>>