DataTable dispose ?

K

Kalpesh

Hi,

Looking at the MSDN documentation, DataTable derives from a class named
"MarshalByValueComponent" - which implements IDisposable.

And, as a better practice - it is good to call .Dispose() on any class
that implements IDisposable

But, to my surprise, MSDN documentation or any sample doesnt explicity
make a call to .Dispose() when using instances of either DataTable,
DataSet, DataColumn etc

Can someone explain ?

Thanks
Kalpesh
 
M

Miha Markic [MVP C#]

Kalpesh said:
Hi,

Looking at the MSDN documentation, DataTable derives from a class named
"MarshalByValueComponent" - which implements IDisposable.

And, as a better practice - it is good to call .Dispose() on any class
that implements IDisposable

Yes. Note that components added through designer are disposed automatically.
But, to my surprise, MSDN documentation or any sample doesnt explicity
make a call to .Dispose() when using instances of either DataTable,
DataSet, DataColumn etc

Can someone explain ?

Those are just samples about certain feature(s). They are not supposed to be
written properly (error handlind, etc)
 
K

Kalpesh

Miha,

Good to hear from an expert. I read your blog :)
So, if I am using DataTable etc in my code (not dropped using the
designer), I must dispose it

Right ?

Kalpesh
 
M

Miha Markic [MVP C#]

Kalpesh said:
Miha,

Good to hear from an expert. I read your blog :)
:)

So, if I am using DataTable etc in my code (not dropped using the
designer), I must dispose it

Right ?

Sort of. IDisposable is a contract that mean call Dispose when done with the
instance and you are safe if you do.
However, some people might argue that Dispose does nothing on certain
classes such as DataTable which might even be true at the present time. Yet
the implementation of Dispose might change for such a class in the future -
imagine that Dispose actually does something important in .net 3 version of
DataTable and your program will have problems if it doesn't call it.

The bottom line is: if you want to be on the safe side always call Dispose
if class implements IDisposable.
 
K

Kevin Spencer

If you create one programmatically, it doesn't have to be disposed, unless
it is bound to some disposable objects.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.
 
K

Kalpesh

Hi Kevin,

Good to hear from another expert :)
This is what I understand from your sentence

Assuming I have a class which implements IDisposable (& .Dispose()
ofcourse)
Now, If this class instance has a reference to DataTable & if I call
myclassinstace.Dispose()

will this result in DataTable getting disposed automatically without me
writing any explicit code ?
Is this what you want to say ? How does it work unless I write
myDataTable.Dispose() in myIntsance.Dispose() method explicitly ?

Thanks
Kalpesh
 
K

Kalpesh

Hi Miha/Kevin,

Can you explain what Kevin has to say & my questions as well ?

Thanks
Kalpesh
 

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