// A single delegate
public delegate void Exploded(string msg);
// Now public! No more helper functions!
public Exploded explodedList;
// Just fire out the Exploded notification.
public void Accelerate(int delta)
{
if (explodedList != null)
explodedList("Sorry, this car is dead...");
}
}
Notice that we no longer have private delegate member variables encapsulated with custom
registration methods. Because these members are indeed public, the caller can directly access the
explodedList member and resign this type to new Exploded objects and invoke the delegate when-
ever it so chooses:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Agh! No Encapsulation! *****\n");
// Make a Car.
Car myCar = new Car();
C H A P T E R 1 1
■
D E L E G AT E S, E V E N T S, A N D L A M B D A S
362
8849CH11.qxd 10/1/07 10:43 AM Page 362
Do'stlaringiz bilan baham: |