Do event handlers stop garbage collection from occurring?

后端 未结 4 1525
离开以前
离开以前 2020-11-22 06:01

If I have the following code:

MyClass pClass = new MyClass();
pClass.MyEvent += MyFunction;
pClass = null;

Will pClass be garbage collected

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 06:18

    Yes, pClass will be garbage collected. The event subscription does not imply that any reference exists to pClass.

    An so no, you will not have to detach the handler in order for pClass to be garbage collected.

提交回复
热议问题