Difference between Finalize() and Dispose()
Finalize() is called by the Garbage Collector before an object that is eligible for collection is reclaimed. Garbage collector will take the responsibility to deallocate the memory for the unreferenced object. The Garbage Collector calls this method at some point after there are no longer valid references to that object in memory.
The framework does not guarantee that when this will happen, you can force for Garbage Collection but it will hurt performance of your program. Finalize() belongs to the Object class and it will be called by the runtime.
There are some resources like windows handles, database connections , network connections, files, etc. which cannot be collected by the Garbage Collector. If you want to explicitly release some specific objects then this is the best to implement IDisposable and override the Dispose() method of IDisposable interface.
The Dispose() method is not called automatically and you must explicitly call it from a client application when an object is no longer needed. Dispose() can be called even if other references to the object are alive.
It is recommends that you implement both Dispose() and Finalize() when working with unmanaged Objects.
- Difference between NameSpace and Assembly
- Execution process for managed code
- What is an Asssembly Qualified Name
- How to Assembly versioning
- Add and remove an assembly from GAC
- What is Native Image Generator (Ngen.exe)?
- How to force Garbage Collection
- Difference between CType and DirectCast
- Obsolete or Deprecated in NET Framework
- What is lazy initialization
- Difference between Clone and Copy
- Difference between static and constant