Following function giving compilation error "Do not override object.Finalize. Instead, provide a destructor."
protected override void Finalize()
{
this.Dispose();
base.Finalize();
}
<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .
%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With