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
}
}
 
Ad

Advertisements

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

Similar Threads

C# and destructors 10
Destructors in C#???? 8
Unsure about Something 19
about Interface IDisposable 3
Question about IDispose 9
C# Destructor Blueprint 4
unsure about object disposal 2
n00b question about destructors 9

Top