Test Your Knowledge: Exercises
Exercise 17-1. Write a countdown alarm program that uses delegates to notify anyone who is interested that the designated amount of time has passed. You’ll need a class to simulate the countdown clock that accepts a message and a number of seconds to wait (supplied by the user). After waiting the appropriate amount of time, the countdown clock should call the delegate and pass the message to any registered observers. (When you’re calculating the time to wait, remember that Thread.Sleep( ) takes an argument in milliseconds, and requires a using System.Threading statement.) Create an observer class as well that echoes the received message to the console.
Exercise 17-2. Change the program you wrote in Exercise 17-1 to ensure that the event can be published to multiple handlers safely.
Exercise 17-3. Rewrite the observer class in Exercise 17-2 to use an anonymous method.
Exercise 17-4. Rewrite the observer class in Exercise 17-3 to use a lambda expression instead of an anonymous method.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access