Do you need to remove an event handler in the destructor?

后端 未结 9 1063
情书的邮戳
情书的邮戳 2021-01-31 01:22

I use some UserControls which get created and destroyed within my application during runtime (by creating and closing subwindows with these controls inside).
It

9条回答
  •  北荒
    北荒 (楼主)
    2021-01-31 02:13

    Firstly I would say do not use a destructor but Dispose() to clear your resources.

    Secondly, in my opinion, if this code is inside an object that is created very often and has a short lifetime, it's better to take care of removing the event handler yourself as this is a link to the holder object, which will prevent the GC from collecting it.

    Regards.

提交回复
热议问题