What is close method? How its different from Finalize & Dispose?

Posted by Deepak under C# on 2/5/2009 | Views : 6592 | Status : [Member] | Replies : 2
What is close method? How its different from Finalize & Dispose?




Responses

Posted by: Learner on: 2/5/2009 [Member] Starter

Up
0
Down
The basic difference between Close() and Dispose() is, when a Close() method is called, any managed resource can be temporarily closed and can be opened once again. It means that, with the same object the resource can be reopened or used. Where as Dispose() method permanently removes any resource ((un)managed) from memory for cleanup and the resource no longer exists for any further processing.

The practical example is creating connection object and calling it's close and then reusing the same connection object to open again.

For more info go through the below link:

http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=23749
http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=58714

Deepak, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sen2zen on: 4/6/2009 [Member] Starter

Up
0
Down
Garbage collector:
garbage collector is responisble to allocate and de-allocate the memory space for managed resources.although garbage collector is able to track the lifetime of an object that encapsulates an unmanaged resources ,but it does not have specific knowledge about how to clean up the unmanaged resources.So to clean up the unmanaged resouces we need to use Dispose() or Finalize().
Close():
when a Close() method is called, any managed resource can be temporarily closed and can be opened once again. It means that, with the same object the resource can be reopened or used
Dispose():
Dispose method is explicitly called by the programmer to free up unmanaged resources,like file handles,database collections.The class implementing Dispose method should implement IDisposable Interface.
Finalize():
Finalize method is implicitly called by the garbage collector to clean up the unmanaged resources by the garbage collector.Finalize method can automatically called by the Garbage collector.But Dispose method can be called by using custom code written by the programmer.To implement the Finalize method we need to use Destructor Syntax.(~)tilde symbol.





Deepak, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response