The finalizer's goal here is just a warning against memory leaks (unless you need to explicitly call Dispose ). It also means that you do not need to delete objects if you want them to free resources when the program shuts down, since the GC will forcefully terminate and collect all objects anyway.
As a connected point, it is important to dispose of the object in a slightly different way when it is done from the finalizer.
~MyClass() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected void Dispose(disposing) { if (!this.disposed) { if (disposing) {
The reason you donβt want to remove managed resources in your finalist is that you will actually create strong links for them to them, and this may prevent the GC from doing the job correctly and collecting them. Unmanaged resources (e.g. Win32 descriptors, etc.) should always be explicitly closed / deleted, of course, since the CLR does not know about them.
Noldorin
source share