长期以来,我让垃圾收集器做它的魔法,从我的责任中解脱出来。
可悲的是,它从未成为问题......所以我从未对这个主题考虑过第二次。
现在当我想到它时,我真的不明白“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。 - Powerlord