C#的委托区别 Action,Func, Predicate
来源:互联网 发布:淘宝客服专员岗位职责 编辑:程序博客网 时间:2024/06/11 20:58
Action无返回值4种重载
Action<T>
Action<T1,T2>
Action<T1,T2,T3>
Action<T1,T2,T3,T4>
用于执行一个带参数的操作,并且不返回值。
示例如下以Action<T>为例
public static void Main(){ Action<string> messageTarget; //方法是实例化 Action<T> 委托,而不是显式定义一个新委托并将命名方法分配给该委托。 messageTarget = ShowWindowsMessage; messageTarget("Hello, World1!"); // Action<T> 委托与匿名方法一起使用 messageTarget = delegate(string s) {ShowWindowsMessage(s); }; messageTarget("Hello, World2!"); //将 lambda 表达式分配给 Action<T> 委托实例 messageTarget = s => ShowWindowsMessage(s); messageTarget("Hello, World3!");} private static void ShowWindowsMessage(string message){ MessageBox.Show(message); }
Func返回值保存在TResult中5种重载,同时Func的返回类型也为TResult
Func<TResult>
Func<T,TResult>
Func<T1,T2,TResult>
Func<T1,T2,T3,TResult>
Func<T1,T2,T3,T4,TResult>
用于执行一个带参数的操作,并且有返回值,返回值保存在TResult中。
示例如下以Func<T,TResult>为例
public static void Main(){ // 实例化 Func<T, TResult> 委托 Func<string, string> convertMethod = UppercaseString; string name = "Dakota1"; Console.WriteLine(convertMethod(name)); //将 Func<T, TResult> 委托与匿名方法一起使用 Func<string, string> convert = delegate(string s) { return UppercaseString(s);}; string name = "Dakota2"; Console.WriteLine(convert(name)); //将 lambda 表达式分配给 Func<T, TResult> 委托。 Func<string, string> convert = s => UppercaseString(s); string name = "Dakota"; Console.WriteLine(convert(name)); }private static string UppercaseString(string inputString){ return inputString.ToUpper();}
Predicate<T>返回值为bool无重载,顾名思义用于对象比较,多用于在集合中搜索元素。
static void Main(string[] args){string Text = "abcdefg";//实例化Predicate<string> 委托Predicate<string> FindA; FindA = ContainA; Console.WriteLine(FindA(Text));//将 Predicate<string> 委托与匿名方法一起使用FindA = delegate(string s) { return ContainA(s); };Console.WriteLine(FindA(Text));//将 lambda 表达式分配给 Predicate<string> 委托。FindA = s=>ContainA(s);Console.WriteLine(FindA(Text));}public static bool ContainA(string Text){ return Text.IndexOf("a") > 0;}
0 0
- C#的委托区别 Action,Func, Predicate
- C#的泛型委托Predicate/Func/Action
- C#令人惊喜的泛型委托Predicate、Func、Action
- C#委托的介绍(delegate、Action、Func、predicate)
- C#委托的介绍(delegate、Action、Func、predicate)
- C#委托的介绍(delegate、Action、Func、predicate)
- C#委托的介绍(delegate、Action、Func、predicate)
- C#委托的介绍(delegate、Action、Func、predicate)
- C#委托的介绍(delegate、Action、Func、predicate)
- C#委托的介绍(delegate、Action、Func、predicate)
- C#委托的介绍(delegate、Action、Func、predicate)
- C#委托的介绍(delegate、Action、Func、predicate)
- C#委托的介绍(delegate、Action、Func、predicate)
- C# 四种委托 Delegate Action Func Predicate 的学习
- C#委托的介绍(delegate、Action、Func、predicate)
- C#委托的介绍(delegate、Action、Func、predicate)
- C#委托的介绍(delegate、Action、Func、predicate)
- C#委托的介绍(delegate、Action、Func、predicate)
- Android资源文件res的使用详解(strings,layout,drawable,arrays等)
- 磨练构建正则表达式模式
- 小米手机数据丢失怎么恢复
- Hadoop MapReduce 二次排序原理及其应用
- 笔记本硬盘数据导出失败怎么恢复
- C#的委托区别 Action,Func, Predicate
- 在MFC中从一个线程工作函数中向窗口发送消息(this指针的妙用) .
- PHP中的$_SERVER["HTTP_REFERER"]用法浅谈
- TCP 传输
- CSS3常用功能的写法
- 您对无法重新创建的表进行了更改或者启用了“阻止保存要求重新创建表的更改”选项
- Reverse Integer
- 浅尝awk
- U盘数据读不出怎么恢复