在C#中处置对象
我写了以下课程:
public class CoupleFrames { public CoupleFrames(ColorImageFrame cif,Bitmap df) { this.colorFrame = cif; this.desktopFrame = df; } public ColorImageFrame colorFrame; public Bitmap desktopFrame; } 现在我使用以下代码来处理变量. CoupleFrames cf = new CoupleFrames(frame1,frame2); // some code... cf.colorFrame.Dispose(); cf.desktopFrame.Dispose(); 我不确定这是正确的方法.有人可以建议我处理整个物体的正确方法吗? 解决方法
当然 – 你应该让CoupleFrames实现IDisposable,它的Dispose方法应该处理它“拥有”的对象.例如: public sealed class CoupleFrames : IDisposable { private readonly ColorImageFrame colorFrame; private readonly Bitmap desktopFrame; public CoupleFrames(ColorImageFrame cif,Bitmap df) { // TODO: Argument validation,unless it's valid for these parameters // to be null,in which case the Dispose method would need to be careful. this.colorFrame = cif; this.desktopFrame = df; } public void Dispose() { colorFrame.Dispose(); desktopFrame.Dispose(); } } 需要注意的几点: >你应该确保CoupleFrame确实“拥有”这些组成对象.处置依赖于明确的所有权模式 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |