#383 – Removing a Handler from an Event

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;

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: