当前位置:网站首页>Unity C# 委托——事件,Action,Func的作用和区别
Unity C# 委托——事件,Action,Func的作用和区别
2022-08-09 06:13:00 【_一只小QQ】
参考视频:
三分钟彻底搞懂委托,事件,Action,Func的作用和区别_哔哩哔哩_bilibili
委托关系图:

Delegate
定义两个模板,一个可以传参一个不可以传参
模板:1、public delegate void xxxxx(int a)
2、public delegate void xxxxx()
注册:符合模板的函数便可以用+=来进行添加
存在的风险:Delegate存在着可以被直接赋值的风险,例如delegate1=delegate2,这样会导致容器内的函数存在丢失的风险。
这个时候如果写成事件Event,赋值操作就不会被允许。
Event
模板:1、public delegate void xxxxx()
2、public event xxxxx delefate1()
但是这么写比较麻烦,所以封装一下写成Action
Action
模板:1、public Action xxxxx()
2、public Action<int> xxxxx()
这里我们没有返回值,如果我们希望有返回值那么就可以使用Function
Func
模板:1、public Func<int> xxxxx() return int
2、public Func<int,bool> xxxxx () return bool
注意:但是若多个返回值发生,则最后保留的是最后注册函数的返回值
边栏推荐
猜你喜欢

51串口通信(上)

sql problem solving statement to create a table

static静态关键字和继承

【Feel】Unity Feel插件中,Camera无法正确显示CameraShake

废品回收小程序、APP UNIAPP开发带有用户端和回收员端

phpstudy 安装 flarum论坛

超顺磁四氧化三铁@二氧化硅@硫化镉纳米核壳结构材料|表面接枝mPEG的Fe3O4磁性纳米颗粒(f-Fe3O4)|相关产品
![[Deep Learning] Let's talk about what is convolutional neural network and what does convolution mean?](/img/12/c3258942e4dcc28714b57d5a3f725c.png)
[Deep Learning] Let's talk about what is convolutional neural network and what does convolution mean?

Unity backgammon game design and simple AI implementation (1)

qt发送邮件程序
随机推荐
MYSQLg advanced ------ batch insert millions of data
Unity backgammon game design and simple AI implementation (1)
Spark源码-任务提交流程-8-DAGScheduler任务切分
zip压缩包密码解密
VB.net程序关闭后后台还在与SQL连接
[Deep Learning] Let's talk about what is convolutional neural network and what does convolution mean?
SiO2/KH550修饰四氧化三铁纳米磁性颗粒|PDA包裹四氧化三铁磁性纳米颗粒(科研级)
Cysteine/Galactose/Perylenediimide Functionalized Fe3O4 Fe3O4 Nanomaterials | Scientific Research Reagents
一文搞懂│XSS攻击、SQL注入、CSRF攻击、DDOS攻击、DNS劫持
【JMeter】jmeter测试 - 上传多个图片/批量上传图片接口 CSV文件参数化方法
契约测试(上):什么是契约测试
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
S7-200SMART PLC Modbus TCP通信
三剑客进阶
ZIP压缩包文件删除密码的方法
el-table缓存数据
程序性能分析 —— 复杂度分析
牛客每日刷题之链表
Three Musketeers Advanced
Functions and differences of command, shell, raw, script modules, application of file, copy, fetch, synchronize