Dispose Method is used for releasing the Unmanaged Resources that hold by any application.
Finalize Method is used for cleaning up the Memory and Resources that is no longer in use.
Below is the Tabular Representation of the Differences in Dispose and Finalize.
Sr.No | Dispose | Finalize |
---|---|---|
1 | Dispose is defined in IDisposable Interface | It is Defined in a Object Class |
2 | It is Used for Releasing up Unmanaged Resources | It is Used for Releasing up Managed Resources. |
3 | It is faster than Finalize | It is Slower than Dispose |
4 | Dispose Method is Invoked by Code | Finalize Method is invoked by Garbage Collector |
5 | The Dispose Method could be Invoked any time | Finalize Method is invoked when reference count goes to zero |
6 | Dispose Method is declared as Public | Finalize Method is Declared as Protected |
The Dispose Method is defined in IDisposable Interface and called explicitly by the programmer to release the Unmanaged resources like forgot to close db connection ,
handling the files etc whereas Finalize Method is Defined inside Garbage Collector Scope
and it called automatically by the Garbage Collector when any resources reference counts goes to Zero , for example any Class has a Object but it is never
assigned any values , so after a long time garbage collector will identify it and will release the Object. We can also excplicitly called the Finalize Method
by program.
Dispose Method is declared as Public whereas Finalize Method is Declared as Protected.
Dispose Method is faster than Finalize Method.
The Finalize Method is Defined in Object Class whereas Dispose Method is Defined in IDisposable Interface.
Dispose Method is Invoked by Code whereas Finalize Method is invoked by Garbage Collector.
The Dispose Method could be Invoked any time whereas Finalize Method is Inviked by Garbage Collector.
Below is the Example Code for Dispose Method to call.
About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the
applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server,
EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.