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;
}
最新文章
- 解析大型.NET ERP系统 灵活复杂的界面控件Infragistics WinForms
- Dom4jUtils.java
- AIX下禁止crs随ha启动而启动
- Git-克隆仓库
- 学习笔记——Maven超级POM
- Power Gating的设计(模块二)
- maven 添加支持编译jdk1.7
- java 最佳且开源的反编译工具
- android进程间通信:使用AIDL
- C#运用实例.读取csv里面的词条,对每一个词条抓取百度百科相关资料,然后存取到数据库
- 写移动端必备的meta标签
- 【Unity Shader实战】卡通风格的Shader(二)
- (转载)Python之道1-环境搭建与pycharm的配置django安装及MySQL数据库配置
- Mysql学习之基础
- UML和模式应用1: 面向对象的分析与设计
- 在Windows Server 2008的桌面上显示“我的电脑”“网上邻居”等图标?
- 51nod1462 树据结构(树链剖分+线段树)
- [LeetCode] 832. Flipping an Image_Easy
- 如何在单片机上使用printf函数(printf)(avr)(stm)(lpc)(单片机)(转)
- Jquery 操作 Select 详解