Func委托和Action委托
http://stackoverflow.com/questions/4317479/func-vs-action-vs-predicate
The difference between Func and Action is simply whether you want the delegate to return a value (use Func) or not (use Action).
Func is probably most commonly used in LINQ - for example in projections:
list.Select(x => x.SomeProperty)
or filtering:
list.Where(x => x.SomeValue == someOtherValue)
or key selection:
list.Join(otherList, x => x.FirstKey, y => y.SecondKey, ...)
Action is more commonly used for things like List<T>.ForEach: execute the given action for each item in the list. I use this less often than Func, although I do sometimes use the parameterless version for things like Control.BeginInvoke and Dispatcher.BeginInvoke.
Predicate is just a special cased Func<T, bool> really, introduced before all of the Func and most of the Action delegates came along. I suspect that if we'd already had Func and Action in their various guises, Predicate wouldn't have been introduced... although it does impart a certain meaning to the use of the delegate, whereas Func and Action are used for widely disparate purposes.
Predicate is mostly used in List<T> for methods like FindAll and RemoveAll.
Func用于有返回值的方法,Action用于没有返回值的方法
Func<string> 表示一个有返回值的函数,返回值为string,但是函数没有参数
Func<stirng,string> 表示一个有返回值的函数,返回值为string,函数参数为string
Action<string> 表示一个无返回值的函数,函数参数为string
Action b = Method;
private static void Method()
{
} Action<string> a = Method;
private static void Method(string str)
{
}
Func<string> a = Method;
private static string Method()
{
return string.Empty;
} Func<string, string> b = Method;
private static string Method(string str)
{
return string.Empty;
}
Func委托和Action委托的更多相关文章
- C#中常见的委托(Func委托、Action委托、Predicate委托)
今天我要说的是C#中的三种委托方式:Func委托,Action委托,Predicate委托以及这三种委托的常见使用场景. Func,Action,Predicate全面解析 首先来说明Func委托,通 ...
- Func 委托 和 Action 委托 初步谈论
继上篇EventHandler之后,继续填坑,简单了解下Func<TResult> 委托 和 Action 委托. msdn对于两者的解释: Func<TResult>:封装一 ...
- [C#学习笔记]Func委托与Action委托
学习一项新知识的时候,最好的方法就是去实践它. 前言 <CLR via C#>这本神书真的是太有意思了!好的我的前言就是这个. Fun 如果要用有输入参数,有返回值的委托,那么Func委托 ...
- C#内置泛型委托:Action委托
1.什么是Action泛型委托 Action<T>是.NET Framework内置的泛型委托,可以使用Action<T>委托以参数形式传递方法,而不用显示声明自定义的委托.封 ...
- .NET : Func委托和Action委托
其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的代码: public delegate void myDeleg ...
- 委托delegate 泛型委托action<> 返回值泛型委托Func<> 匿名方法 lambda表达式 的理解
1.使用简单委托 namespace 简单委托 { class Program { //委托方法签名 delegate void MyBookDel(int a); //定义委托 static MyB ...
- C#系统委托之Action And Func
Action Action<T> Func Func<T> Action:封装一个方法,该方法不具有参数并且不返回值 public delegate void Action() ...
- Func<T>与Action<T>委托泛型介绍:转
.Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...
- Func<T>与Action<T>委托泛型介绍
.Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...
随机推荐
- JDBC、事务和连接池
一:JDBC 1.什么是JDBC JDBC(Java Data Base Connectivity)SUN公司提供的一套操作数据库的标准规范.具体来讲是一种用于执行SQL语句的Java API,为多种 ...
- Spring+mybatis+struts框架整合的配置具体解释
学了非常久的spring+mybatis+struts.一直都是单个的用他们,或者是两两组合用过,今天总算整合到一起了,配置起来有点麻烦.可是配置完一次之后.就轻松多了,那么框架整合配置具体解释例如以 ...
- 对象不支持“abigimage”属性或方法
在一个网页中用了一个js插件, js文件引用的没有错,代码也和demo差点儿相同, 可是执行时ie的调试工具报了一个错: 解决方式: jquery文件冲突,发现原来自己引过一个 <script ...
- pip安装selenium时提示Unknown or unsupported command 'install'
安装流程: 1.安装Python34 2.安装pip 下载setuptoos并安装,然后输入:easy_install pip 然后 配置path:C:\Python34\Scripts 3安装sel ...
- Forms authentication timeout vs sessionState timeout
https://stackoverflow.com/questions/17812994/forms-authentication-timeout-vs-sessionstate-timeout Th ...
- [luogu P4197] Peaks 解题报告(在线:kruskal重构树+主席树 离线:主席树+线段树合并)
题目链接: https://www.luogu.org/problemnew/show/P4197 题目: 在Bytemountains有N座山峰,每座山峰有他的高度$h_i$.有些山峰之间有双向道路 ...
- 安卓-活动Activity
Android有4大组件,活动 Activity,服务 Service ,广播接收器 Brostcast receiver,内容提供器 Content Provider 安卓活动的生命周期有7种, o ...
- Android eclipse 运行项目设置程序默认安装到SD卡
Android eclipse 运行项目设置程序默认安装到SD卡 1.在Android手机启用USB调试功能 2.在Windows系统中打开命令提示符(开始菜单,选择运行,输入cmd回车即可),使用 ...
- Fork and Join: Java Can Excel at Painless Parallel Programming Too!---转
原文地址:http://www.oracle.com/technetwork/articles/java/fork-join-422606.html Multicore processors are ...
- Windows常见软件故障及解决方案
HM NIS Edit: HM NIS Edit 新建程序向导无效,提示“Please specify the setup lang” 说明 NSIS 安装不对.解决方案有二种: 1. 重装 NSIS ...