473,509 Members | 3,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1936
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
3464
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
11857
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
7626
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
6704
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
1637
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
1091
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
5736
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
5136
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
8170
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
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.