Information Technology Reference
In-Depth Information
// Virtual Dispose method
protected virtual void
Dispose(
bool
isDisposing)
{
// Don't dispose more than once.
if
(alreadyDisposed)
return
;
if
(isDisposing)
{
// elided: free managed resources here.
}
// elided: free unmanaged resources here.
// Set disposed flag:
alreadyDisposed =
true
;
}
public void
ExampleMethod()
{
if
(alreadyDisposed)
throw new
ObjectDisposedException
(
"MyResourceHog"
,
"Called Example Method on Disposed object"
);
// remainder elided.
}
}
If a derived class needs to perform additional cleanup, it implements the
protected Dispose method:
public class
DerivedResourceHog
:
MyResourceHog
{
// Have its own disposed flag.
private bool
disposed =
false
;
protected override void
Dispose(
bool
isDisposing)
{
// Don't dispose more than once.
if
(disposed)
return
;
if
(isDisposing)
{
// TODO: free managed resources here.