where to call savetoxml to run on object destruction

D

deciacco

I'm using an object that has a dataset within it.
When the object is instantiated I populate the dataset from xml files.
I want the object to save the dataset back to the xml file before it is
destroyed, but I'm not sure when to call this routine. Should I call it
from the object's destructor or should it implement the IDisposable
interface?

Thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

deciacco,

You should implement the IDisposable interface. In the Dispose method,
you should write the file.

The suggested pattern for implementing IDispose has the finalizer run
the same code (with a variation). For this kind of activity, I would say to
not do this, as it appears that you are looking for specific lifetime
management. Since finalization is non-deterministic, calling the code from
there would be a bad idea.
 
D

deciacco

Thanks for the reply,

Forgive me, but I'm pretty new to C# and I do not understand the second
part.

Are you saying that if I do it, I should use IDisposable, but this isn't
a good way either?
 
N

Nicholas Paldino [.NET/C# MVP]

No, I am saying you should implement IDisposable, but don't place the
logic in the finalizer to write the file.
 

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