Dispose Quick Question

M

Michael C#

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?
 
S

Samuel R. Neff

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


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.
 
M

Michael C#

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
 
H

Herfried K. Wagner [MVP]

Michael C# said:
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
///
 
G

Guest

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?
 
S

Samuel R. Neff

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


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.
 

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

Similar Threads


Top