Finalize()
1.This is the C# equivalent of destructor
~Object()
syntax in C#.
In VB.Net you implement the
Finalize()
by overriding it. But, in C# the compiler translates the destructor to a
Finalize()
method.
2.
Finalize()
can NOT be overridden or called in C#.
3. Since,
Finalize()
is called by the
Garbage Collector, it is non-deterministic.
Dispose()
1. This has to be implemented in classes implementing
IDispose interface
.
2. Its the right place for
freeing-up unmanaged resources like file, handles, and connections etc.
3.
Dispose()
method is called explicitely in the code itself.
4.
Dispose()
method is automatically called (for objects which implement IDispose), when used in a "
using
" statement.
From: here[^]
Refer more detailed description with example on MSDN:
Finalize and Dispose[
^]
Difference:
Dispose Vs Finalize[
^]
Finalize() and Dispose() methods in .Net?[
^]