意義解釋?zhuān)阂粋€(gè)委托包含多個(gè)方法
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)按順序執(zhí)行其中的方法
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.
其實(shí)沒(méi)有明確的順序定義,所以盡量不要寫(xiě)依賴(lài)于執(zhí)行順序的代碼黍析。
2)void
表示
- 加號(hào)“+”表示“添加”
- 減號(hào)“-”表示“ 移除”
coding:
MathOpearations.cs
注:方法返回值為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
Coding:Two
當(dāng)委托中的某個(gè)方法拋出異常時(shí),使用GetInvocationList()方法,保證iterator不停止诬垂,其余的方法也可以迭代完
Result: