✅ How can I get multiple responses from raising an event?
Typically, when you raise an event you don't expect a response. However, you can define you own delegate that has a non-void return type which will mean that a subscriber can return a value. Alternatively, you can pass a mutable object in the
I want to extend this to the case where there are multiple subscribers. The first option doesn't work since you only get the return value of the last subscriber to execute. You can put a
EventArgs which the subscriber can change and then that change can be seen by the publisher.I want to extend this to the case where there are multiple subscribers. The first option doesn't work since you only get the return value of the last subscriber to execute. You can put a
List in the EventArgs and then have the subscribers add their responses to it, however, I don't see any way of doing this that would ensure that each of the subscribers cannot see the responses of other subscribers while allowing the publisher to see all of the responses. The best I have come up with is to have the EventArgs be readable once (e.g. raising an exception or setting a flag if it is tried to be read more than once) which would allow the publisher to see if any of the subscribers have been looking at the other responses. Is there a better way of doing this?