By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,596 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

Dispose

P: n/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
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

"aaj" <aa*@aaj.com> wrote in message
news:1115968104.9b46c80adea6893baec8ff88f22323aa@t eranews...
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

Nov 17 '05 #2

P: n/a

"aaj" <aa*@aaj.com> wrote in message
news:1115968104.9b46c80adea6893baec8ff88f22323aa@t eranews...
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
Nov 17 '05 #3

P: n/a
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.

"aaj" <aa*@aaj.com> wrote in message
news:1115968104.9b46c80adea6893baec8ff88f22323aa@t eranews...
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

Nov 17 '05 #4

P: n/a
Aaj,

See this message from Jay B. which describes it the best I have seen until
now.

http://groups-beta.google.com/group/...b1e6ae59?hl=en

I hope this helps,

Cor
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.