0

I really need to know when to remove event handlers. Does c# remove the event handlers automatically when I'm creating a user control and add them in the template parts of the control?

What about event handlers I add in the Loaded event of a WPF window? Should I remove them in them Closing event of than window? Or C# will do that for me?

What about other scenarios? How much do I care for removing event handlers at all?

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
user3530012
  • 720
  • 2
  • 20
  • 37

2 Answers2

5

All events that you manually subscribe on should also be unsubscribed by you. When depends on when you do not want to receive the notifications any more, but you MUST unsubscribe.

enter image description here

jgauffin
  • 99,844
  • 45
  • 235
  • 372
1

Practically speaking, the safest approach is to unsubscribe from events while you are disposing object that was subscribing this events. Of course there are some cases that you need to think through

Dzior
  • 1,485
  • 1
  • 14
  • 30
  • Could you specifically tell me if I should remove event handlers I add for a FrameworkElementFactory defined in a control template or not? – user3530012 May 09 '14 at 08:18