简体   繁体   English

在C#中如何覆盖Finalize()方法?

[英]In C# how to override the Finalize() method?

Following function giving compilation error "Do not override object.Finalize. Instead, provide a destructor." 以下函数给出编译错误“不要覆盖object.Finalize。而是提供析构函数。”

protected override void Finalize()
{           
    this.Dispose();
    base.Finalize();
}

The finalizer method is called ~name() replacing "name" with your class name. 终结器方法称为~name()用您的类名替换“name”。

The C# compiler will generate the finalizer from this. C#编译器将从中生成终结器。

But note: 但请注意:

  1. Only use a finaliser if you really need it: your type directly contains a native resource (a type composing a wrapper just uses the Dispose pattern). 如果确实需要,只使用终结器:您的类型直接包含本机资源(组成包装器的类型只使用Dispose模式)。
  2. Consider specialising SafeHandle rather than writing your own. 考虑专门化SafeHandle而不是编写自己的。
  3. Implement the dispose pattern to allow callers to release the resource quickly. 实现dispose模式以允许调用者快速释放资源。
  4. Ensure both your Dispose and Finalizer are idempotent—they can be safely called multiple times. 确保您的Dispose和Finalizer都是幂等的 - 可以多次安全地调用它们。

eg 例如

class MyClass : IDisposable {
  private IntPtr SomeNativeResource;

  ~MyClass() {
    Dispose(false);
  }

  public void Dispose() {
    Dispose(true);
  }

  protected virtual void Dispose(bool disposing) {
    if (disposing) {
      // Dispose any disposable fields here
      GC.SuppressFinalize(this);
    }
    ReleaseNativeResource();
  }
}

Subclasses can override Dispose(bool) to add any addition fields they add and call the base implementation. 子类可以重写Dispose(bool)以添加它们添加的任何添加字段并调用基本实现。

EDITED: To add example and notes about when to finalise. 编辑:添加有关何时完成的示例和说明。

You don't. 你没有。

Listen to the compiler. 听取编译器。 You shouldn't override Finalize. 你不应该覆盖Finalize。 Instead, you should implement IDisposible and override Dispose. 相反,您应该实现IDisposible并覆盖Dispose。

Unless you explicitly need to free resources held directly by the object, you should be able to do everything you need to in the Dispose method. 除非您明确需要释放对象直接拥有的资源,否则您应该能够在Dispose方法中执行所需的所有操作。

But if you must: 但如果你必须:

public class MyClass
{
    public MyClass() { ... } // Constructor
    public ~MyClass() { ... } // Destructor/Finalizer
}

Just be careful because Finalizers are tricky and if implemented improperly can carry some pretty hefty performance overhead. 要小心,因为终结器很棘手,如果实施不当可能带来一些相当大的性能开销。

Listen to compiler errors, they are wise beyond their years (unless you really need to, in the rare case, actually mess with the finalizer...the naming's a bit backwards in C#). 听听编译器错误,他们的智慧超出了他们的年限(除非你真的需要,在极少数情况下,实际上是弄乱了终结器......命名在C#中有点倒退)。

You should instead implement Dispose() , making your class implement IDisposable , like this: 您应该实现Dispose() ,使您的类实现IDisposable ,如下所示:

public class MyClass : IDisposable
{
  public void Dispose()
  {
    //cleanup
  }
}

Then when using your class, wrap it in a using , like this: 然后在使用你的类时,将它包装在一个using ,如下所示:

using(var mc = new MyClass()) {
 //use it for things
} //it gets disposed here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM