Any type of unmanaged resource must be supported by a FINALIZATION, and the termination operation allows a resource to perform some cleaning work before the memory it occupies, for this, we must Rewrite a method called Finalize inheriting from Object. If the type does not rewrite the Finalize method, the non-hosting resource will not be closed until the process ends, and the non-hosting resource will be recycled by the operating system. (Of course, we didn't explicitly shut down the non-hosting resources. Of course, we have other auxiliary resource disposal methods, such as implementing the IDisposable interface, using the USING statement when using this object.)
However, in C # cannot call or rewrite the Finalize method, because the C # compile group found that many developers have not been correct enough to the Finalize method, such as the developer forgetting the abnormal situation in the finalize method, for this C # to define the Finalize method Special syntax, that is, the destructor, syntax is similar to the destructor of C --- ~ class name () {}, in the compiled IL code, automatically package it into the Finalize statement. The destructor does not have any parameters, return to the type and access modifier.
Preferably, it is best not to perform a destructuring function. According to the GC's working mode, the object without the destructor will be deleted from memory in one process of GC, but the desired function needs to be deleted.
It should be noted that the CLR does not support deterministic destructors, that is, the Finalize method is only called when the GC is executed, and will not be called when an object exceeds the scope of action. (This is different from C ), so don't reference other managed objects in the destructor, as we cannot expect the object's deletion order.
When designing a class, avoid using the Finalize method as much as possible, if you want to release the unmanaged resource, use the implementation of the Idisposable interface.