Limitations on Plain Use of Delegates

The plain use of delegates for event-based programming seems promising, but a few caveats require a slightly ameliorated approach. As usual, when more than one party is involved in making something work, there needs to be consensus as to how to do things. The exposition of raw delegates is problematic for this reason, if the user of the class does not behave politely. Let’s take a look.

First, multiple parties might want to listen for an event, assuming they all have access to the type raising those events. In other words, we need a way for our users to hook up more than one event handler. That’s easy. Just use multicast delegates with their handy += syntax for combination:

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.