Sparky Mark (sparkymark) wrote in csharp,
Sparky Mark
sparkymark
csharp

Beginner's "-=" question

There are several ways to listen to an event with "+=". As a primarily-Java programmer I'm particulally drawn to the anonymous delegate. But if you want to subsequently remove the listener with -=, how can you identify the object? I guess it is not possible with the anonymous case, but even when you specify

MyEvent += MyInstanceMethod

a delegate for the MyInstanceMethod is being created... so will -= MyInstanceMethod remove the same object from the event's colection of listeners? Or do I have to create an explicit "new" instance of a delegate type, keep it on a field and "-=" the field?
Subscribe
  • Post a new comment

    Error

    default userpic
  • 3 comments