In this article we are going to discuss difference between Dispose and Finalize in C#. There are many blogs, articles are available on the internet regarding Dispose and Finalize but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Dispose and Finalize use in C#.
S.No. | Dispose | Finalize |
---|---|---|
1 | It is defined in the interface IDisposable interface. | It is defined in java.lang.object class. |
2 | It is called explicitly by the developer | It is called by the garbage collector |
3 | It is provides deterministic cleanup of resources. | Non-deterministic cleanup, timing is controlled by the garbage collector. |
4 | No additional GC overhead. | Incur additional performance cost due to GC needing extra cycles. |
5 | It is part of the IDisposable interface, used for both managed and unmanaged resources. | It is used primarily for unmanaged resources, provided by the object class. |
6 | It can be used with a using statement for automatic resource management. | Cannot be used with a using statement. |