By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,668 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,668 IT Pros & Developers. It's quick & easy.

Unsure about destructor & IDisposable for C#

P: n/a
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
}
}
Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

On Sat, 12 Jul 2003 12:37:06 -0400, "Trevor Balcom" <tb*****@attbi.co>
wrote:
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
}
}


Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.