By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

Call .Dispose() Every Time we Can call it?

P: n/a
Should we call .Dispose() every chance we get... on every object that
exposes .Dispose()... and every time we have such an object?

For example, is it true that *every* time I create a DataTable and use it,
that I should call .Dispose of the DataTable instance when I'm done with it?

I was told this is a good .NET programming practice. But I also thought that
..Dispose was there for us to use to clean up objects that actually need
cleaning up - like if we have an object that holds a file handle or database
connection... in that case we'd certainly implement Dispose and cut
connections etc in it. But what about DataTable and other "similar"
..NET-provided objects that apparently don't really hold references to files,
databases, and the like? Is there a good reason to call Dispose on those
objects anyway?

Thanks.

Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
The simple response is that everything you said is 100% correct. You can't
go wrong by calling Dispose (or using Using statements) where allowed, but
in reality if you know that your object isn't using unmanaged resources,
then you don't have to worry about it.

-Scott
"Frank Lund" <A@B.comwrote in message
news:Ou**************@TK2MSFTNGP03.phx.gbl...
Should we call .Dispose() every chance we get... on every object that
exposes .Dispose()... and every time we have such an object?

For example, is it true that *every* time I create a DataTable and use it,
that I should call .Dispose of the DataTable instance when I'm done with
it?

I was told this is a good .NET programming practice. But I also thought
that .Dispose was there for us to use to clean up objects that actually
need cleaning up - like if we have an object that holds a file handle or
database connection... in that case we'd certainly implement Dispose and
cut connections etc in it. But what about DataTable and other "similar"
.NET-provided objects that apparently don't really hold references to
files, databases, and the like? Is there a good reason to call Dispose on
those objects anyway?

Thanks.



Jun 27 '08 #2

P: n/a
If the object implement disposable, we better use

using(Myobject objABC = new Myobject())
{
.....
}
System will clear up all the resource after run the section code
"Frank Lund" <A@B.comwrote in message
news:Ou**************@TK2MSFTNGP03.phx.gbl...
Should we call .Dispose() every chance we get... on every object that
exposes .Dispose()... and every time we have such an object?

For example, is it true that *every* time I create a DataTable and use it,
that I should call .Dispose of the DataTable instance when I'm done with
it?

I was told this is a good .NET programming practice. But I also thought
that .Dispose was there for us to use to clean up objects that actually
need cleaning up - like if we have an object that holds a file handle or
database connection... in that case we'd certainly implement Dispose and
cut connections etc in it. But what about DataTable and other "similar"
.NET-provided objects that apparently don't really hold references to
files, databases, and the like? Is there a good reason to call Dispose on
those objects anyway?

Thanks.


Jun 27 '08 #3

P: n/a
But, the OP is asking if it is necessary, not just good practice and the
answer is that it is not always necessary. For example, a DataSet exposes
Dispose(), but you would only need to call it if you were loading your
DataSet from a file, not if you were loading it from a string.
"CLGan" <cl******@hotmail.comwrote in message
news:B7**********************************@microsof t.com...
If the object implement disposable, we better use

using(Myobject objABC = new Myobject())
{
....
}
System will clear up all the resource after run the section code
"Frank Lund" <A@B.comwrote in message
news:Ou**************@TK2MSFTNGP03.phx.gbl...
>Should we call .Dispose() every chance we get... on every object that
exposes .Dispose()... and every time we have such an object?

For example, is it true that *every* time I create a DataTable and use
it, that I should call .Dispose of the DataTable instance when I'm done
with it?

I was told this is a good .NET programming practice. But I also thought
that .Dispose was there for us to use to clean up objects that actually
need cleaning up - like if we have an object that holds a file handle or
database connection... in that case we'd certainly implement Dispose and
cut connections etc in it. But what about DataTable and other "similar"
.NET-provided objects that apparently don't really hold references to
files, databases, and the like? Is there a good reason to call Dispose on
those objects anyway?

Thanks.



Jun 27 '08 #4

P: n/a
Frank Lund wrote:
Should we call .Dispose() every chance we get... on every object that
exposes .Dispose()... and every time we have such an object?

For example, is it true that *every* time I create a DataTable and
use it, that I should call .Dispose of the DataTable instance when
I'm done with it?
When it's done being used (not just by you but by any library you handed it
off to), yes. This is hardly the same as "every time we can call it" which
you wrote in the subject line.
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.