public Chapter5() { MyEventChap6 evt = new MyEventChap6(); Subscribers1 sub1 = new Subscribers1(); Subscribers2 sub2 = new Subscribers2(); evt.SomeEvent += sub1.Handler1; evt.SomeEvent += sub2.Handler2; evt.OnSomeEvent(); }
public Chapter6() { MyEventChap6 evt = new MyEventChap6(); W wOb = new W(); X xOb = new X(); Y yOb = new Y(); Z zOb = new Z(); // Add handlers to the event list. Console.WriteLine("Adding events."); evt.SomeEvent += wOb.Whandler; evt.SomeEvent += xOb.Xhandler; evt.SomeEvent += yOb.Yhandler; // Can't store this one -- full. evt.SomeEvent += zOb.Zhandler; Console.WriteLine(); // Raise the events. evt.OnSomeEvent(); Console.WriteLine(); // Remove a handler. Console.WriteLine("Remove xOb.Xhandler."); evt.SomeEvent -= xOb.Xhandler; evt.OnSomeEvent(); Console.WriteLine(); // Try to remove it again. Console.WriteLine("Try to remove xOb.Xhandler again."); evt.SomeEvent -= xOb.Xhandler; evt.OnSomeEvent(); Console.WriteLine(); // Now, add Zhandler. Console.WriteLine("Add zOb.Zhandler."); evt.SomeEvent += zOb.Zhandler; evt.OnSomeEvent(); }