11

Possible Duplicate:
C#: How to remove a lambda event handler

Is it possible to remove an event handler which was attached as anonymous function? Let's say I have an event, and I subscribe to it in this way:

TestClass classs = new TestClass ();
classs.myCustomEvent +=  (a,b) => { Console.Write(""); };

Is it possible somehow to remove this eventHandler using -= ??

Community
  • 1
  • 1
magneto
  • 135
  • 1
  • 3

1 Answers1

24

It is possible, but you need to store it in a local variable first:

MyDelegate handler = (a, b) => { Console.Write(""); };
class.myCustomEvent += handler;
class.myCustomEvent -= handler;
fparadis2
  • 913
  • 7
  • 12
  • 3
    Not sure why this was downvoted... +1, this is a correct answer. Except the part about the "local" variable: in most scenarios you will need to store it in a more long lived location... – Thomas Levesque Jun 23 '11 at 20:39