Events

Unfortunately, the universe, being very busy and unaccustomed to paying attention to individuals, had managed to replace Peter's boss's delegate with its own. This was an unintended side effect of making the delegate fields public in Peter's Worker class. Likewise, if Peter's boss got impatient, he could decide to fire Peter's delegates himself (which is just the kind of rude thing that Peter's boss was apt to do):

// Peter's boss taking matters into his own hands
if( peter.completed != null ) peter.completed();

Peter wanted to make sure that neither of these could happen. He realized that he needed to add registration and unregistration functions for each delegate so that subscribers could add or remove themselves but couldn't clear ...

Get Windows Forms Programming in C# now with O’Reilly online learning.

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