Unsure about destructor & IDisposable for C#

T

Trevor Balcom

I would like to do things when my object goes out of scope, like
disconnect TCP/IP streams, serialize collections to files and so on... Would
I implement my cleanup code in Dispose() ?

Is this correct?


class A : IDisposable
{
A()
{
}

~A()
{
Dispose();
}

public void Dispose()
{
//clean up stuff here
}
}
 
J

Jonathan Schafer

That is partially correct. Check out the docs for how MS implements
IDisposable in their objects. The key thing is that if the Dispose
method is called, that you also call GC.SupressFinalize(). You will
also need a flag to indicate that the Dispose method was called vs the
finalizer.

Jonathan Schafer
 

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