Multicast Delegates 多播委托

2019-02-18  本文已影响0人  津涵

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

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
上一篇下一篇

猜你喜欢

热点阅读