c# 委托(Func、Action)
以前自己写委托都用 delegate, 最近看组里的大佬们都用 Func , 以及 Action 来实现, 代码简洁了不少, 但是看得我晕晕乎乎。 花点时间研究一下,记录一下,以便后期的查阅。
1、Func 用法 (封装方法,传入参数, 有返回值)
Func<in T1, in T2, ..., out TResult> (T1, T2, ...)
封装一个方法,该方法有 (0 /1/2/3 ... 16)个参数,且返回由 TResult 参数指定的值的类型。
public static void Main()
{
// 方法一: Func 相当于系统内置的 委托
Func<int, int, string> method = Calculate; // 方法二: 调用 Lambda 方法实现, 更简洁
Func<int, int, string> method_1 = (x, y) =>
{
int val = x + y;
return string.Format("the calculate {0} plus {1} result is: {2}", x, y, val);
}; Console.WriteLine(method(, ));
Console.WriteLine(method_1(, ));
Console.ReadLine();
} public static string Calculate(int x, int y)
{
int val = x + y;
return string.Format("the calculate {0} plus {1} result is: {2}", x, y, val);
}
2、Action 用法 (封装一个方法, 传入参数, 无返回值)
Action<T1, T2, T3, ...>(t1, t2, t3 ...)
封装一个方法, 该方法传入 (0/1/2 ...) 个参数, 且不返回值。
public static void Main()
{
Method_First("Hi, Here!");
Method_First("Hi, There!"); Console.ReadLine();
} private static void Method_First(string y)
{
Action<string> method;
method = x => { Console.WriteLine("the input message is: {0}", x); };
method(y);
} private static void Method_Sec(string y)
{
Action<string> method = x => { Console.WriteLine("the input message is : {0}", x); };
method(y);
}
3. 委托的使用
讲了两种不同情况的委托, 那么什么时候使用委托呢?
根据官方文档,在以下情况下,请使用委托:
当使用事件设计模式时。
当封装静态方法可取时。
当调用方不需要访问实现该方法的对象中的其他属性、方法或接口时。
需要方便的组合。
当类可能需要该方法的多个实现时。
4. 在 Task 使用委托
Task 表示一个异步操作。
public static void Main()
{
// 启动方法1
Task t = Task.Run(() =>
{
Thread.Sleep();
Console.WriteLine("First task finished time is:{0}", DateTime.Now.ToString());
}); // 方法2
Task t_2 = Task.Factory.StartNew(() => {
Thread.Sleep();
Console.WriteLine("second task finished time is:{0}", DateTime.Now.ToString());
}); // 方法 3
Action action = () =>
{
Thread.Sleep();
Console.WriteLine("third task finished time is:{0}", DateTime.Now.ToString());
};
Task.Factory.StartNew(action).ContinueWith(thirdTask =>
{
if (thirdTask.IsCompleted)
{
Console.WriteLine("the third task has finished");
}
else if (thirdTask.IsFaulted)
{
Console.WriteLine(thirdTask.Exception);
}
}); Console.WriteLine("main thread has end:{0}",DateTime.Now.ToString() ); Console.ReadKey();
}
运行结果如下 :
main thread has end:2018-03-04 22:03:39
First task finished time is:2018-03-04 22:03:40
second task finished time is:2018-03-04 22:03:41
third task finished time is:2018-03-04 22:03:42
the third task has finished
c# 委托(Func、Action)的更多相关文章
- 委托、多播委托、泛型委托Func,Action,Predicate,ExpressionTree
当试图通过一个事件触发多个方法,抽象出泛型行为的时候,或许可以考虑使用委托. 通过委托构造函数或委托变量把方法赋值给委托 private delegate double DiscountDel ...
- C#系统委托之Action And Func
Action Action<T> Func Func<T> Action:封装一个方法,该方法不具有参数并且不返回值 public delegate void Action() ...
- C#中常见的委托(Func委托、Action委托、Predicate委托)
今天我要说的是C#中的三种委托方式:Func委托,Action委托,Predicate委托以及这三种委托的常见使用场景. Func,Action,Predicate全面解析 首先来说明Func委托,通 ...
- Func 委托 和 Action 委托 初步谈论
继上篇EventHandler之后,继续填坑,简单了解下Func<TResult> 委托 和 Action 委托. msdn对于两者的解释: Func<TResult>:封装一 ...
- 委托, 泛型委托,Func<T>和Action<T>
使用委托来做一些事情,大致思路是: 1.定义声明一个委托,规定输入参数和输出类型.2.写几个符合委托定义的方法.3.把方法列表赋值给委托4.执行委托 internal delegate int MyD ...
- [C#学习笔记]Func委托与Action委托
学习一项新知识的时候,最好的方法就是去实践它. 前言 <CLR via C#>这本神书真的是太有意思了!好的我的前言就是这个. Fun 如果要用有输入参数,有返回值的委托,那么Func委托 ...
- 委托delegate 泛型委托action<> 返回值泛型委托Func<> 匿名方法 lambda表达式 的理解
1.使用简单委托 namespace 简单委托 { class Program { //委托方法签名 delegate void MyBookDel(int a); //定义委托 static MyB ...
- 对委托 以及 action func 匿名函数 以及 lambda表达式的简单记录
class Program { public delegate void MyDelegate(string str); static void Main(string[] args) { // My ...
- C# 委托应用总结(委托,Delegate,Action,Func,predicate)
C# 委托应用总结 一.什么是委托 1.1官方解释 委托是一种定义方法签名的类型.当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联.您可以通过委托实例调用方法. 1.2个人理解 委托就是执 ...
- 浅谈C#中常见的委托<Func,Action,Predicate>(转)
一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...
随机推荐
- PMS 启动流程
1.在SystemServer中启动PackageManagerService.main 2.newPackageManagerService()并添加到ServiceManager中 3.newin ...
- nginx配置中root与alias的区别
nginx指定文件路径有两种方式root和alias,这两者的用法区别,使用方法总结了下,方便大家在应用过程中,快速响应.root与alias主要区别在于nginx如何解释location后面的uri ...
- MySQL相关命令与备份
不加任何参数直接备份 mysqldump -uroot zabbix >/opt/zabbix.bak.sql 恢复,这样恢复时需要自已创建表 mysql -uroot < zabbix. ...
- 珍爱生命,远离JS=>JS避坑记
JavaScript避坑记 转载请注明源地址: http://www.cnblogs.com/funnyzpc/p/8407952.html 上图=> 有意思的漫画,不知大家看懂了没,这里我想说 ...
- 4.Handler之CoreHandler编写
4.Handler之CoreHandler编写 如图右上角所示,Ray中有两类Handler(SubHandler和PartSubHandler),在使用中,SubHandler派生Actor的Cor ...
- 用记事本编写java中的HelloWorld
一.安装并配置jdk(图片来自百度经验) 安装JDK 选择安装目录 安装过程中会出现两次 安装提示 .第一次是安装 jdk ,第二次是安装 jre .建议两个都安装在同一个java文件夹中的不同文件夹 ...
- malloc,calloc,realloc,alloc
三个函数的申明分别是: void* realloc(void* ptr, unsigned newsize); void* malloc(unsigned size); void* calloc(si ...
- web.config文件中配置数据库连接的两种方式
web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报 分类: 数据库(74) 在网站开发 ...
- python正则实现简单计算器
利用正则实现计算器 利用正则来实现简单计算器的功能,能够设计计算带括号的加减乘除运算.当然不使用eval等语句. 利用递归: import re from functools import reduc ...
- c#监测电脑状态
public class DeviceMonitor { static readonly PerformanceCounter cpuCounter = new PerformanceCounter( ...