views:

374

answers:

1

hi all

i am completely confused about close, dispose, finalize, GC, Idisposable. Oh, could you please send me a clear description of them?

+4  A: 

That is a fairly large topic. May I recommend the book CLR via C# by Richter. It goes into details about all the issues you mention.

A very brief translation:

  • On disposable types Close is often the same as Dispose.
  • Dispose is used to allow deterministic clean up of resources not handled by garbage collection.
  • Finalizer (or destructor as the C# language spec calls it) is a clean-up method called by the garbage collection code at some point in time. I.e. unlike destructors for C++ this is not done at a well defined point in time.
  • GC is short for garbage collection and refers to .NET's automatic clean-up of objects located on the managed heap.
  • IDisposable is an interface, that states that the type in question implements the Dispose method as outlined above.

For more details please consult the book. This rather elaborate blog entry by Joe Duffy is also very useful for understanding IDisposable and finalizers.

Brian Rasmussen
Thanks Brian, when should we implement a finalize method for a class?Or Is that really necessary to implement it?
odiseh
I have updated my answer with a link to Joe Duffy's excellent treatment of that question, so please see that. The short answer is: You shoud rarely finalizers. If your classes only uses managed resources you most likely will not need finalizers.
Brian Rasmussen
+1 for your good comment.
odiseh