Search code examples
c#.netdelegatesmulticastdelegate

Use of multicast in C# multicast delegates


When is it useful to have multicast delegates over singlecast delegates?

I use delegates a lot, mainly coupled with C# lambdas, but I've never felt the urge to use the multicast aspect of C# delegates, ie I've never wanted to combine multiple delegates together in a single delegate. I'm therefore very curious in what sort of situation multicast delegates are useful - I can only think of examples in which you can easily implement the functionality some other way, by, say, chaining the delegates or putting them in a list.

In particular, Eric Lippert's answer here gives the impression even the C# team sometimes forgets about the multicastiness of delegates.


Solution

  • Anything acting as an event is the classic answer here - then the caller doesn't need to know who is listening (just invoke it, if it is non-null). This is ideal as multiple operations can subscribe simultaneously - for example 3 separate controls observing (data-binding to) the same property on a view-model.

    Any cases where the delegate is acting as a function (in particular with a return value) is trickier, as you need to think about how you will handle that - take the first? Last? Aggregate?