473,395 Members | 1,652 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,395 software developers and data experts.

Dispose Quick Question

The System.Data.SqlClient.SqlConnection has Close() and Dispose() methods.
So I started using Dispose() on my SqlConnections after I Close() them.
I've started getting into the habit of calling Dispose() on every object
that has a Dispose() method on it, after I'm through with it. I was just
wondering if it's safe to assume that if an object has a Dispose() method
you should always call it when done with it? Or am I just making more
busy-work for myself and having no effect on anything really?
Nov 21 '05 #1
5 1036

If you control the objec, then you should dispose all IDisposable
objects. Control usually means if you created it, but that's not
always the case.

Dispose calls Close so you don't need to call both. Just call
Dispose.

Besides being good practice, Dispose also usually triggers
GC.SuppressFinalize() and will actually improve the garbage collector
performance since the finalizer no longer has to be called (not always
the case, but is the norm for Dispose implementations).

HTH,

Sam
On Wed, 16 Mar 2005 13:02:36 -0500, "Michael C#" <xy*@yomomma.com>
wrote:
The System.Data.SqlClient.SqlConnection has Close() and Dispose() methods.
So I started using Dispose() on my SqlConnections after I Close() them.
I've started getting into the habit of calling Dispose() on every object
that has a Dispose() method on it, after I'm through with it. I was just
wondering if it's safe to assume that if an object has a Dispose() method
you should always call it when done with it? Or am I just making more
busy-work for myself and having no effect on anything really?


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 21 '05 #2
Thanks for the tip. BTW, is there a construct in VB similar to the C#
"using" keyword, that I could use with the SqlDataReader?

Thanks again

"Samuel R. Neff" <bl****@newsgroup.nospam> wrote in message
news:ap********************************@4ax.com...

If you control the objec, then you should dispose all IDisposable
objects. Control usually means if you created it, but that's not
always the case.

Dispose calls Close so you don't need to call both. Just call
Dispose.

Besides being good practice, Dispose also usually triggers
GC.SuppressFinalize() and will actually improve the garbage collector
performance since the finalizer no longer has to be called (not always
the case, but is the norm for Dispose implementations).

HTH,

Sam
On Wed, 16 Mar 2005 13:02:36 -0500, "Michael C#" <xy*@yomomma.com>
wrote:
The System.Data.SqlClient.SqlConnection has Close() and Dispose() methods.
So I started using Dispose() on my SqlConnections after I Close() them.
I've started getting into the habit of calling Dispose() on every object
that has a Dispose() method on it, after I'm through with it. I was just
wondering if it's safe to assume that if an object has a Dispose() method
you should always call it when done with it? Or am I just making more
busy-work for myself and having no effect on anything really?


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.

Nov 21 '05 #3
"Michael C#" <xy*@yomomma.com> schrieb:
Thanks for the tip. BTW, is there a construct in VB similar to the C#
"using" keyword, that I could use with the SqlDataReader?


VB.NET 2002/2003 don't provide such a construct, but VB 2005 will include
'Using' too. 'using' can be replaced by this "pattern":

\\\
Dim Connection As SqlConnection
Try
Connection = New SqlConnection()
Connection.Open()

' Use the connection here.
Finally
If Not Connection Is Nothing Then
Connection.Dispose()
End If
End Try
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
I thought that calling the .close method would call the dispose method. If I
understand you correctly, then I should use .dispose instead of .close. I
this correct?

"Samuel R. Neff" wrote:

If you control the objec, then you should dispose all IDisposable
objects. Control usually means if you created it, but that's not
always the case.

Dispose calls Close so you don't need to call both. Just call
Dispose.

Besides being good practice, Dispose also usually triggers
GC.SuppressFinalize() and will actually improve the garbage collector
performance since the finalizer no longer has to be called (not always
the case, but is the norm for Dispose implementations).

HTH,

Sam
On Wed, 16 Mar 2005 13:02:36 -0500, "Michael C#" <xy*@yomomma.com>
wrote:
The System.Data.SqlClient.SqlConnection has Close() and Dispose() methods.
So I started using Dispose() on my SqlConnections after I Close() them.
I've started getting into the habit of calling Dispose() on every object
that has a Dispose() method on it, after I'm through with it. I was just
wondering if it's safe to assume that if an object has a Dispose() method
you should always call it when done with it? Or am I just making more
busy-work for myself and having no effect on anything really?


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.

Nov 21 '05 #5

Whether or not there is a real advantage in calling Dispose instead of
Close will depend on the object, but it is always true that you can
call Dipose instead of Close.

However, not all objects expose a public Dipsose method--they often
explicitly implement Dispose through the interface, so to call it you
have to direct-cast the object to IDisposable. Not a big deal, just
something to be aware of.

HTH,

Sam
On Wed, 16 Mar 2005 16:19:01 -0800, Dennis
<De****@discussions.microsoft.com> wrote:
I thought that calling the .close method would call the dispose method. If I
understand you correctly, then I should use .dispose instead of .close. I
this correct?

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Shawn Brock | last post by:
I exhaustively dispose/close anything database related and have managed to control connection leaks. However, I was wondering about the necessity to use Dispose on non-database resources. 1. ...
4
by: Johannes Hansen | last post by:
What are the best practice on handling an exception caused by a Dispose method when its called from inside a loop? Wrap the entire loop in a try-catch or do the try-catch on each iteration to get...
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...
15
by: Sam Sungshik Kong | last post by:
Hello! A disposable object's Dispose() method can be called either explicitly by the programmer or implicitly during finalization. If you call Dispose, the unmanaged resources are released...
3
by: Ed | last post by:
I just learned how to use the Commandbuilder and have researched it a little bit further and see that there is a dispose property. Should this be used after invoking the command builder or does...
6
by: Teresa | last post by:
1) If I do want to keep an object alive throughout the live of an application, how can I ensure that the GC doesn't clean it up? 2a) How do I determine if an object is a managed or an unmanged...
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...
3
by: AlexS | last post by:
When I implement Dispose pattern in object implementing IDisposable, current fxcop recommends: Ensure that Wrapper.Dispose():Void is declared as public and sealed. However, if I do as it asks,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.