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

语法

public delegate void Action<in T1, in T2>(
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<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();
}
}
}

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

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

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

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

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

Action<T1, T2>委托的更多相关文章

  1. 使用Func<T1, T2, TResult> 委托返回匿名对象

    Func<T1, T2, TResult> 委托 封装一个具有两个参数并返回 TResult 参数指定的类型值的方法. 语法 public delegate TResult Func< ...

  2. 使用Func<T1, T2, TResult>

    使用Func<T1, T2, TResult> 委托返回匿名对象   Func<T1, T2, TResult> 委托 封装一个具有两个参数并返回 TResult 参数指定的类 ...

  3. Aap.Net中的Action和Func委托

    前言 最近在阅读某开源框架源码的时候,发现作者在其中运用了很多Action委托和Func委托,虽然我之前在项目中也有一些对委托的实操,但还是免不了长时间的不用,当初消化的一些委托基础都遗忘了...索性 ...

  4. C#扫盲篇(三):Action和Func委托--实话实说

    一.基础定义 老王想找老张的老婆出去耍,但是一看,老张还在厨房煮饭.于是老王就对老张隔壁的淑芬说:"等下老张吃完饭出去喝茶,你就把前门晒的苞谷收了,老张从左门出,你就收右边的苞谷,我就知道从 ...

  5. Func<T1, T2, TResult> Delegate 系统Func委托类型

    原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb534647%28v=VS.1 ...

  6. 匿名方法、Lambda表达和自定义泛型委托以及Func、Action系统泛型委托

    1.匿名方法的概念:一个方法没有具体的名称,而只有关键字delegate.方法参数.方法体.这种方法是匿名方法. 匿名方法的好处:将具体方法和委托直接关联在一起,如果我们基于委托只需要一个方法的时候, ...

  7. Action<>和Func<> 委托【代理】

    C#中的Action<>和Func<> 其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的 ...

  8. DateTime.Compare(t1,t2)比較两个日期大小

    DateTime.Compare(t1,t2)比較两个日期大小,排前面的小,排在后面的大,比方:2011-2-1就小于2012-3-2返回值小于零:  t1 小于 t2. 返回值等于零 : t1 等于 ...

  9. java 多线程,T1 T2 T3 顺序执行

    一.程序设计 1.抽象公共类PublicThread,具有先前线程属性previousThread.父类为Thread 2.在PublicThread的run()方法中判断previousThread ...

随机推荐

  1. 验证页面多个input文本的必填项

    前台页面 JS : function CheckMustWrite(){ var count = $("input[mustwrite = 'true']", document.f ...

  2. MySQL高可用之MHA搭建

    测试环境 节点1 172.16.200.231 6666               master         节点2 172.16.200.27 6666 slave1              ...

  3. Debugging Process Startup

    Debugging Process Startup Q:  How do I debug a process's startup code? A: This depends on how the pr ...

  4. M3: 将页面元素制作为图片

    本小节将介绍如何将页面元素保存为图片,在前一小节中,我们加入了名称为gridMsg的Grid Control,现在我们将使用RenderTargetBitmap把gridMsg这个页面元素保存为一张图 ...

  5. Golang Clearing slice

    //first method : slice = nil // second method : slice = slice[0:0] Source page : https://www.socketl ...

  6. 一种Go使用tcp检测超时的方式

    c.SetReadDeadline(time.Now()) if _, err := c.Read(one); err == io.EOF { l.Printf(logger.LevelDebug, ...

  7. LintCode Count 1 in Binary

    知识点 1. 整数的二进制表示法 2. 十进制和二进制的转换 http://baike.baidu.com/view/1426817.htm 3. 负整数的表示(原码,补码,反码) http://ww ...

  8. Android学习五:Content Provider 使用

    1ContentProvider相关知识1.1在安卓应用中,通过文件方式对外共享数据,需要进行文件操作读写数据:采用sharedpreferences共享数据,需要使用sharedpreference ...

  9. 基于MVC4+EasyUI的Web开发框架形成之旅--权限控制

    我在上一篇随笔<基于MVC4+EasyUI的Web开发框架形成之旅--框架总体界面介绍>中大概介绍了基于MVC的Web开发框架的权限控制总体思路.其中的权限控制就是分为“用户登录身份验证” ...

  10. world machine, 输出lightmap

    一,输出黑白lightmap: 二,输出彩色lightmap: 需要注意的是:当输出黑白lightmap时,输出设备要用Height Output:当输出彩色lightmap时,输出设备要用Bitma ...