Multicast Delegates 多播委托

意义解释:一个委托包含多个方法

A delegate that wraps more than one method is known as a multicast delegate.

When called:

When a multicast delegate is called, it successively calls each method in order. For this to work, the delegate signature should return a void; otherwise, you would only get the result of the last method invoked by the delegate.
1)按顺序执行其中的方法
If you are using multicast delegates, be aware that the order in which methods chained to the same delegate will be called is formally undefined. Therefore, avoid writing code that relies on such methods being called in any particular order.
其实没有明确的顺序定义,所以尽量不要写依赖于执行顺序的代码。
2)void

表示

  1. 加号“+”表示“添加”
  2. 减号“-”表示“ 移除”

coding:

MathOpearations.cs


1.png

注:方法返回值为void,用输出的方式代替有返回值
You now need the delegate to refer to methods that return void, you rewrite the methods in the MathOperations class so they display their results instead of returning them.

Main.cs


2.png

3.png

Coding:Two

当委托中的某个方法抛出异常时,使用GetInvocationList()方法,保证iterator不停止,其余的方法也可以迭代完


4.png

Result:


5.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容