【发布时间】:2011-03-31 10:40:24
【问题描述】:
长期以来,我让垃圾收集器发挥作用,从我自己身上卸下所有责任。
遗憾的是它从来没有变成一个问题......所以我从来没有考虑过这个主题。
现在回想起来,我真的不明白“dispose”函数的真正作用以及应该如何以及何时实现它。
finalize 同样的问题...
最后一个问题... 我有一个类pictureManipulation:当我需要保存/调整大小/更改格式时...我启动该类的一个新实例,使用它的对象并且...让垃圾收集杀死实例
class student
{
public void displayStudentPic()
{
PictureManipulation pm = new PictureManipulation();
this.studentPic = pm.loadStudentImage(id);
}
}
Class Test
{
student a = new Student();
a.displayStudentPic();
// Now the function execution is ended... does the pm object is dead? Will the GC will kill it?
}
【问题讨论】:
-
作为一般规则,如果您使用的任何对象实现了
IDisposable,您应该实现它。或者,如果您使用的是 P/Invoke。
标签: c# dispose garbage-collection finalize