Finalize and Dispose are both known as Finalizers.
They are the methods used to deallocate the memory of the objects
1)Finalize belongs to the Object class.
2)It is automatically called by the Garbage Collection mechanism when the object goes out of the scope(usually at the end of the program)
It is not called by the User Code.
In C#, suppose there is a class known as emp
//This is the destructor of emp class
//This destructor is implicitly compiled to the Finalize method.
3)Not suitable for instant disposing of the objects.Sloer method.
1)Dispose belongs to the IDisposable interface
2)we have to manually write the code to implement it(User Code)
ex: if we have emp class we have to inherit it from the IDisposable interface
and write code. We may have to suppress the Finalize method using GC.SuppressFinalize() method.
3)Faster method for instant disposal of the objects
Example: user interface Controls. Forms, SqlConnection class have built in implementaion of Dispose method.
Asked In: Many Interviews |