Home > DeveloperSection > Forums > What is the difference between Finalize() and Dispose()?
John William
John William

Total Post:5

Points:35
Posted on    November-11-2010 1:29 AM

 C# C# 
Ratings:


 4 Reply(s)
 4377  View(s)
Rate this:
Please Explain


Amit Singh

Total Post:103

Points:721
Posted on    March-16-2011 12:52 AM

Difference between Finalize() and Dispose() method
Finalize :
1.Finalize() is called by the runtime
2.Is a destructor, called by Garbage Collector when the object goes out of scope.
3.Implement it when you have unmanaged resources in your code, and want to make sure that these resources are freed when the Garbage collection happens.

Dispose :
1.Dispose() is called by the user
2.Same purpose as finalize, to free unmanaged resources. However, implement this when you are writing a custom class, that will be used by other users.
3.Overriding Dispose() provides a way for the user code to free the unmanaged objects in your custom class.

Dispose() method permanently removes connection object from memory and the resource no longer exists for any further          processing.



Modified On Mar-16-2011 01:00:11 AM

Awadhendra Tiwari

Total Post:126

Points:882
Posted on    March-16-2011 1:14 AM

Design Pattern : If your classes use unmanaged resources, you need to implement both Dispose & Finalize. Dispose() is
called by user code, that is, the code that is using your class.

Finalize/Destructor cannot be called by User code, it's called by Garbage
Collector

Finalize : Is a destructor, called by Garbage Collector when the object goes
out of scope. Implement it when you have unmanaged resources in your code, and
want to make sure that these resources are freed when the Garbage collection
happens.
Dispose : Same purpose as finalize, to free unmanaged resources. However,
implement this when you are writing a custom class, that will be used by other
users. Overriding Dispose() provides a way for the user code to free the
unmanaged objects in your custom class
               


Lakshmi Narayanan
Lakshmi Narayanan

Total Post:5

Points:35
Posted on    October-07-2011 1:30 AM

Hi,
 refer this link
http://www.geekinterview.com/question_details/37506

yang welar
yang welar

Total Post:1

Points:7
Posted on    May-27-2013 4:08 AM

Finalize()

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.

Dispose()

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.

source:

http://net-informations.com/faq/framework/finalize-dispose.htm

 

yang.


Don't want to miss updates? Please click the below button!

Follow MindStick