.net中的各种委托(Delegate、Action、Func)
2024-08-25 22:16:02
1、Delegate,委托的鼻祖
protected delegate int ClassDelegate(int x, int y);//定义委托类型及参数
static void Main(string[] args)
{
ClassDelegate dele = new ClassDelegate(Add);//实例化一个委托 Console.WriteLine(dele(, ));//调用委托
Console.ReadKey();
} static int Add(int a, int b)
{
return a + b;
}
2、Action,可以传入参数,没有返回值的委托
方法1,调用方法
static void Main(string[] args)
{
Action<int, int> ac = new Action<int, int>(ShowAddResult);//实例化一个委托
ac(, );//调用委托 Console.ReadKey();
} static void ShowAddResult(int a, int b)
{
Console.WriteLine(a + b);
}
方法2,使用lambda表达式
static void Main(string[] args)
{
Action<int, int> ac = ((p, q) => Console.WriteLine(p + q));//实例化一个委托
ac(, );//调用委托 Console.ReadKey();
}
方法3,作为参数传
static void Main(string[] args)
{
Action<string> ac = (p => Console.WriteLine("我是方法1,传入值:"+p));//实例化一个委托
Action<string> ac2 = (p => Console.WriteLine("我是方法2,传入值:" + p));//实例化另一个委托 Test(ac, "参数1");//调用test方法,传入委托参数
Test(ac2, "参数1");//调用test方法,传入委托参数 Console.ReadKey();
} static void Test<T>(Action<T> ac, T inputParam)
{
ac(inputParam);
}
3、Func,可以传入参数,必须有返回值的委托
方法1,调用方法
static void Main(string[] args)
{
Func<string> fc1 = new Func<string>(ShowAddResult);//实例化一个委托
string result = fc1();//调用委托 Console.WriteLine(result);
Console.ReadKey();
}
static string ShowAddResult()
{
return "地球是圆的";
}
方法2,使用lambda表达式
static void Main(string[] args)
{
//实例化一个委托,注意不加大括号,写的值就是返回值,不能带return
Func<string> fc1 = () => "地球是圆的"; //实例化另一个委托,注意加大括号后可以写多行代码,但是必须带return
Func<string> fc2 = () =>
{
return "地球是圆的";
}; string result = fc1();//调用委托
string result2 = fc2();//调用委托 Console.WriteLine(result);
Console.WriteLine(result2);
Console.ReadKey();
}
方法3,作为参数传
static void Main(string[] args)
{
//实例化一个委托,注意不加大括号,写的值就是返回值,不能带return
Func<int, string> fc1 = (p) => "传入参数" + p + ",地球是圆的"; //实例化另一个委托,注意加大括号后可以写多行代码,但是必须带return
Func<string, string> fc2 = (p) =>
{
return "传入参数" + p + ",地球是圆的";
}; string result = Test<int>(fc1, );//调用委托
string result2 = Test<string>(fc2, "");//调用委托 Console.WriteLine(result);
Console.WriteLine(result2);
Console.ReadKey();
} static string Test<T>(Func<T, string> fc, T inputParam)
{
return fc(inputParam);
}
总结:
Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型
Func可以接受0个至16个传入参数,必须具有返回值
Action可以接受0个至16个传入参数,无返回值
最新文章
- Microsoft Visual Studio 插件
- iOS 如何在Label中显示html的文本
- mongodb 使用场景和不使用场景
- stm32调试记录一
- ios修改textField的placeholder的字体颜色和大小
- mongo学习笔记(六):linux上搭建
- SQL Server锁、闩等资源的阻塞诊断---osql/sqlcmd,sp_blocker_pss80
- 织梦dedecms中html和xml格式的网站地图sitemap制作方法
- mysql日志详细解析 [转]
- Memcached缓存
- Runloop -------iOS
- RTMP消息详细介绍
- 初识Restful架构
- Eslint检测出的问题如何自动修复
- 异常处理与MiniDump 用于投放市场c++异常捕获
- “一切都是消息”--iMSF(即时消息服务框架)入门简介
- python命令行解析模块--argparse
- Bias(偏差),Error(误差),和Variance(方差)的区别和联系
- php实现简单的单链表
- [sh]uniq-sort-awk
热门文章
- Android初级教程图片信息
- Java中把JSON和List结果集互转的代码片段整理
- 网站开发进阶(三十五)JSP页面中的pageEncoding和contentType两种属性
- go-mysql,一个易用的mysql接口框架实现
- (NO.00001)iOS游戏SpeedBoy Lite成形记(二十二)
- ISLR系列:(3)重采样方法 Cross-Validation &; Bootstrap
- Python学习笔记 - 高阶函数
- android的Binder通信机制java层浅谈-android学习之旅(88)
- window环境下搭建react native及相关插件
- 【Qt编程】基于Qt的词典开发系列<;三>;--开始菜单的设计