Difference between Finalize and Dispose

This blog shows the difference between Finalize and Dispose


Finalize :

·         Finalize() is called by the runtime

·         Is a C# equivalent of destructor, called by Garbage Collector when the object goes out of scope.

·         Implement it when you have unmanaged resources in your code, and want to make sure that these resources are freed when the Garbage collection happens.

·         Finalize() can NOT be overridden or called in C#.

·         Since, Finalize() is called by the Garbage Collector, it is non-deterministic.

 

Dispose :

·         Dispose() is called by the user

·         Same purpose as finalize, to free unmanaged resources. However, implement this when you are writing a custom class, that will be used by other users.

·         Overriding Dispose() provides a way for user code to free the unmanaged objects in your custom class.

·         Dispose() has to be implemented in classes implementing IDispose interface.

·         Dispose() method is called explicitly in the code itself.