Action<>和Func<> 区别
2016-01-11 16:55
351 查看
其实他们两个都是委托【代理】的简写形式。
一、【action<>】指定那些只有输入参数,没有返回值的委托
Delegate的代码:
[csharp] view plaincopy
public delegate void myDelegate(string str);
public static void HellowChinese(string strChinese)
{
Console.WriteLine("Good morning," + strChinese);
Console.ReadLine();
}
myDelegate d = new myDelegate(HellowChinese);
d("Mr wang");
用了Action之后呢:
[csharp] view plaincopy
public static void HellowChinese(string strChinese)
{
Console.WriteLine("Good morning," + strChinese);
Console.ReadLine();
}
Action<string> action = HellowChinese;
action("Spring.");
就是相当于省去了定义委托的步骤了。
二、func<> 这个和上面的那个是一样的,区别是这个有返回值!
[csharp] view plaincopy
public static string HelloEnglish(string strEnglish)
{
return "Hello." + strEnglish;
一、【action<>】指定那些只有输入参数,没有返回值的委托
Delegate的代码:
[csharp] view plaincopy
public delegate void myDelegate(string str);
public static void HellowChinese(string strChinese)
{
Console.WriteLine("Good morning," + strChinese);
Console.ReadLine();
}
myDelegate d = new myDelegate(HellowChinese);
d("Mr wang");
用了Action之后呢:
[csharp] view plaincopy
public static void HellowChinese(string strChinese)
{
Console.WriteLine("Good morning," + strChinese);
Console.ReadLine();
}
Action<string> action = HellowChinese;
action("Spring.");
就是相当于省去了定义委托的步骤了。
二、func<> 这个和上面的那个是一样的,区别是这个有返回值!
[csharp] view plaincopy
public static string HelloEnglish(string strEnglish)
{
return "Hello." + strEnglish;
相关文章推荐
- 关于在SharePoint 2013(2010)中Javascript如何实现批量批准的自定义操作功能?
- 【Java】————概述
- DNS服务器搭建从协议到实现详解
- SQL Update多表联合更新的方法
- C# 插入或删除word分页符
- SVN项目下载后报错 maven-resources-plugin prior to 2.4 is not supported by m2e.
- iOS监测第三方键盘高度
- OC协议
- CodeForces 540A Combination Lock
- [web安全] 服务器端请求伪造(SSRF)
- 华为机试——数字奇偶排序
- IOS绘制圆,直线,弧线,矩形,扇形,三角形,贝塞尔等图形
- Android SDK无法更新终极解决方式
- 批量梯度下降与随机梯度下降
- 定义与声明的区别
- mac 和 windows 连接远程服务器及文件传送
- MapReduce学习之好友推荐
- 我为Net狂 ~ 社交平台系列小集合!
- 从今天起记录一下自己看过的书
- 最详细的Log4j使用教程