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

后端 未结 9 1041
情书的邮戳
情书的邮戳 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:02

    The GC will take care of that. While the event holds a strong reference, it only holds it on the parent object itself. In the end only MyControl will keep a reference via the event handler, thus the GC will collect it.

    On the otherhand using the finalizer, which is NOT a descrutor. Is for this bad practice. If you want to unregister an event, you should consider IDisposable.

提交回复
热议问题