在 C# 中处理对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17315967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:09:01  来源:igfitidea点击:

Disposing object in C#

c#.netdispose

提问by GVillani82

I have written the following class:

我编写了以下课程:

public class CoupleFrames
{
    public CoupleFrames(ColorImageFrame cif, Bitmap df)
    {
        this.colorFrame = cif;
        this.desktopFrame = df;
    }

    public ColorImageFrame colorFrame;
    public Bitmap desktopFrame;
}

Now I'm using the following code for disposing the variables.

现在我使用以下代码来处理变量。

CoupleFrames cf = new CoupleFrames(frame1, frame2);
// some code...
cf.colorFrame.Dispose();
cf.desktopFrame.Dispose();

I'm not sure that this is the correct way. Someone can suggest me the correct way for disposing the entire object?

我不确定这是正确的方法。有人可以建议我处理整个对象的正确方法吗?

采纳答案by Jon Skeet

I'm not sure that this is the correct way. Someone can suggest me the correct way for disposing the entire object?

我不确定这是正确的方法。有人可以建议我处理整个对象的正确方法吗?

Sure - you should make CoupleFramesimplement IDisposable, and itsDisposemethod should dispose of the objects it "owns". For example:

当然 - 你应该 make CoupleFramesimplement 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();
    }
}

A few points to note:

需要注意的几点:

  • You should make sure it's clear that the CoupleFramereally "owns" these constituent objects. Disposal relies on a clear ownership model
  • If CoupleFrameisn'tsealed (and can't be) you may need to go into a more complicated pattern with virtual methods and finalizers. It can get very complicated, and you should read the advice given here by Joe Duffy et al. If your class is sealed, a lot of that complexity goes away
  • Public fields are generally a bad idea (in terms of encapsulation), which is why I've made them private here. I've also made them readonly, as if they can be changed later you need to think about whether changing them should dispose of the previously-referenced object etc.
  • By making CoupleFrameimplement IDisposable, you're basically telling all clients that they should dispose of any instance they own. If you're not happy with imposing that burden, you need to rethink the design a bit.
  • 您应该确保清楚CoupleFrame真正“拥有”这些组成对象。处置依赖于清晰的所有权模式
  • 如果CoupleFrame没有密封(并且不能密封),您可能需要使用虚拟方法和终结器进入更复杂的模式。它可能会变得非常复杂,您应该阅读Joe Duffy 等人在此处给出建议。如果您的课程是密封的,那么很多复杂性就会消失
  • 公共字段通常是一个坏主意(就封装而言),这就是我在这里将它们设为私有的原因。我也将它们设为只读,就好像它们可以在以后更改一样,您需要考虑更改它们是否应该处理先前引用的对象等。
  • 通过实现CoupleFrameimplement IDisposable,您基本上是在告诉所有客户端他们应该处理他们拥有的任何实例。如果您对施加这种负担不满意,则需要重新考虑一下设计。

回答by Avi Turner

Make CoupleFrames Implement the Idisposable Interface.

使 CoupleFrames 实现 Idisposable 接口。

public class CoupleFrames : IDisposable
{
    public CoupleFrames(ColorImageFrame cif, Bitmap df)
    {
        this.colorFrame = cif;
        this.desktopFrame = df;
    }

    public ColorImageFrame colorFrame;
    public Bitmap desktopFrame;

    public void Dispose()
    {
        cf.colorFrame.Dispose();
        cf.desktopFrame.Dispose();
    }

回答by Hyman Hughes

Implement IDisposable in your class and have it dispose of the member variables from there.

在您的类中实现 IDisposable 并让它从那里处理成员变量。

public class CoupleFrames : IDisposable
        {
            public CoupleFrames(ColorImageFrame cif, Bitmap df)
            {
                this.colorFrame = cif;
                this.desktopFrame = df;
            }

            public ColorImageFrame colorFrame;
            public Bitmap desktopFrame;

            public void Dispose()
            {
                this.colorFrame.Dispose();
                this.desktopFrame.Dispose();
            }
        }

回答by Sri Harsha Chilakapati

You can use the IDisposableinterface.

您可以使用该IDisposable界面。

public class CoupleFrames : IDisposable
{
    ....

    public void Dispose()
    {
        // Your disposing code here
    }

    ~CoupleFrames()
    {
        Dispose();
    }
}

You can use the destructor to call the Dispose method since the object can sometimes be deleted by the GC.

您可以使用析构函数调用 Dispose 方法,因为该对象有时会被 GC 删除。

回答by rexcfnghk

I would implement the Dispose pattern

我会实现Dispose 模式

public class CoupleFrames : IDisposable
{
    public CoupleFrames(ColorImageFrame cif, Bitmap df)
    {
        this.colorFrame = cif;
        this.desktopFrame = df;
    }

    public ColorImageFrame colorFrame;
    public Bitmap desktopFrame;

    private bool disposed;

    public void Dispose()
    {
        Dispose(true);
        GC.SupressFinalize(this); 
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposed)
        {
            return;
        }
        if (disposing)
        {
            colorFrame.Dispose();
            desktopFrame.Dispose();
        }
        disposed = true;
    }
}