473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Dispose

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
4 2355
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
4
by: RiteshDotNet | last post by:
..net Frame work 1. Dispose Method what it does ? A. who its call / when it calls ? B. Is it fire automatically ? c. When dispose method is call what it does ? D. Release a Object from memory or...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
3
by: Maxim | last post by:
Hi! According to documenation, if we need to release some umanaged resources manually, we need to implement IDisposable interface with single Dispose method. I am just wondering, what will...
1
by: Billy | last post by:
Hello... I'm trying to make a database access class for an asp.net application. When I run my application, the Garbage Collecter doesn't seems to unload the memory attributed to my...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.