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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset