Concept Flow - Multicast delegates
Create delegate instance
Add method1 to delegate
Add method2 to delegate
Invoke delegate
Call method1
Call method2
End
A multicast delegate holds references to multiple methods and calls them in order when invoked.
delegate void Notify(); void Method1() => Console.WriteLine("Method1 called"); void Method2() => Console.WriteLine("Method2 called"); Notify notify = Method1; notify += Method2; notify();
| Step | Action | Delegate State | Output |
|---|---|---|---|
| 1 | Create delegate notify with Method1 | notify -> [Method1] | |
| 2 | Add Method2 to notify | notify -> [Method1, Method2] | |
| 3 | Invoke notify() | Calls Method1 then Method2 | Method1 called |
| 4 | Method2 called after Method1 | All methods called in order | Method2 called |
| 5 | End of delegate invocation | No more methods to call |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| notify | null | [Method1] | [Method1, Method2] | [Invoked] | [Invoked] |
Multicast delegates hold multiple method references. Use += to add methods. Invoke calls all methods in order. Return value is from last method only. Exceptions stop further calls.