Action<T1, T2>委托
封装包含两个参数的方法委托,没有返回值。
语法
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>委托的更多相关文章
- 使用Func<T1, T2, TResult> 委托返回匿名对象
Func<T1, T2, TResult> 委托 封装一个具有两个参数并返回 TResult 参数指定的类型值的方法. 语法 public delegate TResult Func< ...
- 使用Func<T1, T2, TResult>
使用Func<T1, T2, TResult> 委托返回匿名对象 Func<T1, T2, TResult> 委托 封装一个具有两个参数并返回 TResult 参数指定的类 ...
- Aap.Net中的Action和Func委托
前言 最近在阅读某开源框架源码的时候,发现作者在其中运用了很多Action委托和Func委托,虽然我之前在项目中也有一些对委托的实操,但还是免不了长时间的不用,当初消化的一些委托基础都遗忘了...索性 ...
- C#扫盲篇(三):Action和Func委托--实话实说
一.基础定义 老王想找老张的老婆出去耍,但是一看,老张还在厨房煮饭.于是老王就对老张隔壁的淑芬说:"等下老张吃完饭出去喝茶,你就把前门晒的苞谷收了,老张从左门出,你就收右边的苞谷,我就知道从 ...
- Func<T1, T2, TResult> Delegate 系统Func委托类型
原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb534647%28v=VS.1 ...
- 匿名方法、Lambda表达和自定义泛型委托以及Func、Action系统泛型委托
1.匿名方法的概念:一个方法没有具体的名称,而只有关键字delegate.方法参数.方法体.这种方法是匿名方法. 匿名方法的好处:将具体方法和委托直接关联在一起,如果我们基于委托只需要一个方法的时候, ...
- Action<>和Func<> 委托【代理】
C#中的Action<>和Func<> 其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的 ...
- DateTime.Compare(t1,t2)比較两个日期大小
DateTime.Compare(t1,t2)比較两个日期大小,排前面的小,排在后面的大,比方:2011-2-1就小于2012-3-2返回值小于零: t1 小于 t2. 返回值等于零 : t1 等于 ...
- java 多线程,T1 T2 T3 顺序执行
一.程序设计 1.抽象公共类PublicThread,具有先前线程属性previousThread.父类为Thread 2.在PublicThread的run()方法中判断previousThread ...
随机推荐
- 在 Vultr VPS 中 以 Debian 8 i386 (jessie) 为 操作系统 平台 手动 搭建 PPTP VPN 全过程
更新服务器并安装 PPTP 服务 apt-get update apt-get upgrade apt-get install pptpd 编辑 /etc/pptpd.conf 找到 #locali ...
- 关于ADDED_TO_STAGE事件
可视类初始化的时候,很多时候要用到stage属性,则要使用Event.ADDED_TO_STAGE事件,这个swf被其它的文件加载,如果直接在初始化函数内使用stage属性 .但是,文档类初始化函数内 ...
- 关于一些学习html和css的笔记
一.Html简介 全写: HyperText Mark-up Language 译名: 超文本标识语言 简释:一种为普通文件中某些字句加上标示的语言,其目的在于运用标签(tag)使文件 达到预期的 ...
- c# windows编程控件学习-1
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- bzero函数
函数原型:void bzero(void *s,int n) 作用:bzero函数的作用是将s指针指向的地址的前n个字节清零. 头文件:#include <string.h> eg.
- [CDN]CDN的系统架构
---恢复内容开始--- 1.功能架构: CDN技术自1998年诞生以来,伴随着互联网的高速发展,其技术一直在持续演进和完善,但基本的CDN功能架构在2003年左右就已基本形成和稳定下来.从功能上划分 ...
- 3、通过挂在系统光盘搭建本地yum仓库的方法
1. mkdir xxx #新建文件夹 (新建一个挂载需要的文件夹) .配置本地yum源(挂载光盘) .进入 yum.repos.d .ls (查看当前文件夹全部的文件) 并 mv 修改 除Med ...
- How To Use Hbase Bulk Loading
最近在学习hbase,学到利用如何将数据导入到hbase中,采用的方式是批量导入:bulk load的方法,中间出现了一些问题,下面将执行的步骤记录一下,以供日后查阅: 说明:导入的方式是将csv文件 ...
- net_device 结构体分析
/* * The DEVICE structure. * Actually, this whole structure is a big mistake. It mixes I/O * data wi ...
- Understanding Linux /proc/cpuinfo
http://www.richweb.com/cpu_info A hyperthreaded processor has the same number of function units as a ...