events under the hood

Is it important to remove the same eventlistener object as you add Is essence, what is the difference between these two examples

Ex 1.
someObj.MyEvent += new Class1.MyEventHandler(obj_MyEvent);
...
someObj.MyEvent -= new Class1.MyEventHandler(obj_MyEvent);


Ex 2.
Class1.MyEventHandler d = new Class1.MyEventHandler(someObj_MyEvent);
someObj.MyEvent += d;
...
someObj.MyEvent -= d;

Both seems to accomplish the same thing but in the first example I provide the -= operator with a different object than I added. But both objects are given the same function. Do i get any side effects when using the first example Is there a best practice for this


Answer this question

events under the hood

  • dantheriver

    The two examples are completely equivalent.
    I would prefer the first one since the code is more clear to understand.

  • events under the hood