Should I be using .Dispose()?

M

Michael

Should I make calls to .Dispose() for ado.net objects when I'm done with
them, or is it best to let .net's garbage collector handle these objects
after I'm done with them?

Thanks
 
T

Tu-Thach

I think microsoft recommends that you call Dispose when
you are done with that object. If you are using C#, you
can easily do that with the using keyword.

Tu-Thach
 
M

Miha Markic

Hi,

As Tu-Thach said, it doesn't hurt (acutally it is very recommended) that you
invoke Dispose() at the end.
However, the actuall instance is handled by GC later on even if you invoke
Dispose.
 
K

Kathleen Dollard

Michael,

What objects are you worried about? Most ADO.NET objects do not support the
IDisposable interaface.
 
C

Cor

Hi Michael,

When you at home bring every plate, spoon etc to the kitchen as soon as you
see that somebody is eating nomore, yes dispose. If you wait until everybody
is ready, just wait and let the system do it in one time.

However, many people are thinking that it is more efficient to bring every
part separately to the kitchen.

:)

Cor
 
B

bruce barker

yes.

idispose is implemented when an object uses unmanaged resources. if its a
limited resource (such as file handle), failing to release them in a timely
manner can cause problems.

-- bruce (sqlwork.com)
 
D

David Browne

Michael said:
Should I make calls to .Dispose() for ado.net objects when I'm done with
them, or is it best to let .net's garbage collector handle these objects
after I'm done with them?

Thanks

As others have noted many of the ADO.NET object inherit IDisposable from
Component. So you really do need advice on what to dispose.

Here it is:

Dispose Connections and DataReaders.

Nothing else needs to be disposed.

David
 
W

William \(Bill\) Vaughn

There is no need (no improvement in performance or memory management) to use
Dispose on any ADO.NET object--even Connections or DataReaders.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
M

Mike Junkin

You can either close the connections and data readers or dispose them
- but you have to one or the other.

In general if an object has a Dispose method it's there for a reason
and your better off using it.

And, of course, Dispose can be automatically managed - via the 'using'
operator. To insure the close you'll have to wrap everything with
exception handling. But that's more a matter of style as they both
get the job done.

Mike


William (Bill) Vaughnwrote:
There is no need (no improvement in performance or memory management)
to use
Dispose on any ADO.NET object--even Connections or DataReaders.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

message
Should I make calls to .Dispose() for ado.net objects when I'm done with
them, or is it best to let .net's garbage collector handle these objects
after I'm done with them?

Thanks


As others have noted many of the ADO.NET object inherit IDisposable from
Component. So you really do need advice on what to dispose.

Here it is:

Dispose Connections and DataReaders.

Nothing else needs to be disposed.

David

[/quote:5235641a42]
 
D

David Browne

William (Bill) Vaughn said:
There is no need (no improvement in performance or memory management) to use
Dispose on any ADO.NET object--even Connections or DataReaders.

--

IDisposable.Dispose on these objects just runs .Close(). And in C#, this is
invoked with the "using" keyword.

You would, of course, agree that running .Close on DataReaders and
Connections is mandatory.

David
 
W

William \(Bill\) Vaughn

Of course. But I think calling Close where needed is better than calling
Dispose so it will call Close for you. Why? Because it's less confusing for
those just getting into C#. Close is vitally important. Dispose (for
ADO.NET) is not.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
D

David Browne

William (Bill) Vaughn said:
Of course. But I think calling Close where needed is better than calling
Dispose so it will call Close for you. Why? Because it's less confusing for
those just getting into C#. Close is vitally important. Dispose (for
ADO.NET) is not.

Fair enough. How many times have people posted here: "I have run out of
connections in my connection pool, but I always close my connections."
Chances are they just missed one.

The "using" keyword is to take the guesswork and hastle out of disposable
objects. Nowhere is this more important than ADO.NET connections.

I would advice C# developers to always use Connections and DataReaders in
using blocks.

David
 
W

William \(Bill\) Vaughn

I'm for that. In VB.NET (2.0) Using now works for VB as well. ;)

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 

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

Top