Hi,
What is the main difference between Finalize() and Dispose()?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Mar 14, 2012, 1:50 AM
Finalize and Dispose are both known as Finalizers.
They are the methods used to deallocate the memory of the objects
Differences:
FINALIZE
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
class emp
{
//This is the destructor of emp class
~emp()
{
}
//This destructor is implicitly compiled to the Finalize method.
}
3)Not suitable for instant disposing of the objects.Sloer method.
DISPOSE
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.
Thanks
SenthilkumarPosted Mar 14, 2012, 1:45 AM
The simply you can say the finalize is no guarantee to run and it happens implicitly.Where Dispose is explicitly called by the developers.