Following function giving compilation error \"Do not override object.Finalize. Instead, provide a destructor.\"
protected override void Finalize()
{
The finalizer method is called ~name()
replacing "name" with your class name.
The C# compiler will generate the finalizer from this.
But note:
SafeHandle
rather than writing your own.e.g.
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.
EDITED: To add example and notes about when to finalise.