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

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

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
4 1924
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nigel T | last post by:
Hi, I have a form containing controls, a timer, and the EventLog component. I also create some new instances of classes that do not implement idisposable I don't want to leave the tidying up to...
3
by: David N | last post by:
Hi All, I just wonder if in C#, I can develop a user defined control that can call its parent function which is not yet developed. For example, how do I make my user control call a...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
14
by: wobbles | last post by:
Hi Everyone, I've read most of the posts regarding "Dispose" in this NG. I still have some questions. If I declare and use a disposable object (say a DB connection) within a method, is it...
13
by: | last post by:
I'm curious if anyone knows why the C# and VB.NET compilers don't automatically call Dispose() on objects that support IDisposable when they go out of scope. I asked a co-worker and his response...
1
by: Sam Sungshik Kong | last post by:
Hello! I am creating a user control. It draws something in OnPaint method every 0.5 second. Instead of creating a Pen object and disposing it every time in the method, I create a Pen object in...
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...
54
by: Zytan | last post by:
I have a log class that makes a synchronized TextWriter like so, in the constructor: StreamWriter sw = new StreamWriter(filename); tw = TextWriter.Synchronized(sw); In the destructor,...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.