When to use IDispose


S

ssg31415926

If I have a class which wraps a class which implements IDisposable,
should I implement IDisposable, too, in the wrapping class, and call
the wrapped class Dispose() from the wrapping class Dispose()?

E.g.

Public Class Widget : IDisposable
{
private DisposableObject disposableObject;

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
disposableObject.Dispose()
}
}
~Widget()
{
Dispose(false);
}
 
Ad

Advertisements

T

The Crow

yes. if you read .net framework library documentation, the example in the
IDisposable.Dispose() method does same thing as yours. (example uses
private Component component; rather then DisposableObject. this is the only
difference with yours)
 
Ad

Advertisements

S

ssg31415926

I think I must be having an off-day - I didn't spot the sample code in
there until your reply made me go and look again!

Thanks very much

SSG
 

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