Dispose

A

aaj

Hi

can anyone point to a good link explaining (simply) when I should use the
dispose method.

Sort of things I'd like to know are

how does it differ from finalize in VB.net (or is it nothing like finalize)
Does the garbage collector automatically call the dispose method when an
object goes out of scope.
Does every object support it, if not, which do
Does it have to be called manually for every object that does support it
....

this sort of thing

thanks

Andy
 
V

Vijaye Raji

1. Finalize is different from Dispose. Finalize is like a destructor, while
Dispose is like a Free() call (sort of)
2. The garbage collector will not call the Dispose method
3. Not every object supports Dispose. Any class that implements IDisposable
will support it.
4. Yes, you need to call it manually (or use the "using" keyword, in which
case the compiler will generate code to call Dispose)

Dispose is where you would free up any resource that you are hanging on to.
That would be a good place to close files or close any Database connections
you have open. Not every object needs it.

-vj
 
M

Michael S

aaj said:
Hi
Hi

can anyone point to a good link explaining (simply) when I should use the
dispose method.

Ther are plenty Just google.
Sort of things I'd like to know are

how does it differ from finalize in VB.net (or is it nothing like
finalize)
- It is nothing like finalize.
Does the garbage collector automatically call the dispose method when an
object goes out of scope.
- Nope. You have to do that yourself. Have a look at the using keyword.
Does every object support it, if not, which do
- Typically classes that uses external resources like SqlConnection and
StreamReader. FxCop is a nifty tool that will tell you if you missed calling
Dispose on such objects.
Does it have to be called manually for every object that does support it
- Yep. (again see the using keyword)
thanks

Andy

Andy, how the GC works, and what makes finalizers in .NET so very different
from a destructor in unmanaged runtimes like C++ and Delphi; is of grave
importance. You really should take a break from coding and start reading all
about it. This will teach you why we need the Dispose pattern in .NET.

Happy Coding, err Reading...
- Michael S
 
B

Bob Powell [MVP]

My philosophy is that if an object is disposable it's probably for a reason.
If Dispose exists, use it when your done with the object.

Only objects that implement IDisposable will have Dispose methods.

Finalize may call Dispose but that isn't certain and will depend upon the
person that wrote the object.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top