Lesson 10: Events and Observer
Without events, a class that wants to notify others must know each of them — and every new listener forces you to open it and edit it again. In this lesson you learn how C# `event` and `+=` flip the direction: interested parties subscribe themselves, and the publishing class knows none of them.
An event is like a newspaper's mailing list: the paper knows none of its readers personally, yet everyone who signed up gets the issue the moment it comes out.
- Event and the Observer pattern
- A mechanism where a publishing class raises an event and subscribers registered with += get notified — without the publisher knowing them.
- event subscription (+=)
- += adds a handler to the event's subscriber list, and -= removes it. Direct assignment with = is forbidden outside the publishing class.
- EventHandler (delegate)
- The delegate type that fixes the signature of every event subscriber — for example EventHandler<string> takes a sender and a string.