Do I need to unsubscribe events in my Form?

前端 未结 4 729
不知归路
不知归路 2020-12-19 17:23

I\'m trying to understand how a Control events are unsubscribed. Suppose I have a textbox and I have subscribed the TextChanged event using the Win

4条回答
  •  鱼传尺愫
    2020-12-19 17:32

    Yes, you better unsubscribe. As official docs say (here)

    In order to prevent resource leaks, you should unsubscribe from events before you dispose of a subscriber object. Until you unsubscribe from an event, the multicast delegate that underlies the event in the publishing object has a reference to the delegate that encapsulates the subscriber's event handler. As long as the publishing object holds that reference, garbage collection will not delete your subscriber object.

提交回复
热议问题