#383 – Removing a Handler from an Event
August 5, 2011 Leave a comment
You can use the += operator to add a handler to an event. Because an event can invoke multiple handlers, you can use the += operator to add additional handlers to the same event. When the event is raised, all of the handlers will be called.
kirby.Barked += kirby_Barked; kirby.Barked += kirby_Barked2;
static void kirby_Barked(object o, EventArgs e) { Console.WriteLine("I see that Kirby barked"); } static void kirby_Barked2(object o, EventArgs e) { Console.WriteLine("I ALSO see that Kirby barked"); }
It’s also possible to remove a particular handler for the list of methods that an event will invoke, using the -= operator.
kirby.Barked -= kirby_Barked;