PC Review


Reply
Thread Tools Rate Thread

Dispose Quick Question

 
 
Michael C#
Guest
Posts: n/a
 
      16th Mar 2005
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?


 
Reply With Quote
 
 
 
 
Samuel R. Neff
Guest
Posts: n/a
 
      16th Mar 2005

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#" <(E-Mail Removed)>
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.
 
Reply With Quote
 
Michael C#
Guest
Posts: n/a
 
      16th Mar 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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#" <(E-Mail Removed)>
> 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.



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      16th Mar 2005
"Michael C#" <(E-Mail Removed)> 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/>

 
Reply With Quote
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      17th Mar 2005
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#" <(E-Mail Removed)>
> 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.
>

 
Reply With Quote
 
Samuel R. Neff
Guest
Posts: n/a
 
      17th Mar 2005

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
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick question - quick answer about assigning shortcut keys funkymonkUK Microsoft Excel Programming 1 13th Oct 2005 10:50 AM
Dispose Question Scott Hembrough Microsoft Dot NET Framework 1 22nd Nov 2004 05:46 PM
Question about Dispose Bob Lehmann Microsoft ADO .NET 21 21st May 2004 08:54 PM
Question about Dispose Bob Lehmann Microsoft ASP .NET 21 21st May 2004 08:54 PM
Dispose Question? David W Microsoft C# .NET 1 22nd Mar 2004 04:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:56 PM.