No, you cannot "assign" a delegate to an event handler. Handlers are attached to events, adding them to the call list of the main delegate, which is used internally to represent the event. This is by design!
And no, you cannot change the handler by changing the object pointed to by the link previously used to attach the event handler; partly because delegates are immutable , and partly because you just change the link to point to something else without changing the event handler you are trying to execute.
To change a delegate, you must first delete the previous delegate:
backgroundworker.DoWork -= dweh;
Then assign a new one, adding it as an event handler:
backgroundworker.DoWork += new DoWorkEventHandler(method2);
Note
In most cases, you can remove the handler (delegate) from the event using this syntax:
backgroundworker.DoWork -= new DoWorkEventHandler(mehtod1);
or using implicit or explicit method group transformation:
backgroundworker.DoWork -= (DoWorkEventHandler)mehtod1;
But depending on the situation, you may need a link to the previous delegate to subsequently delete it. For example, this applies to anonymous methods or lambda expressions.
source share