
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Disposing of Unmanaged Resources
|
213
disposed; a true confirms that it has been disposed. This flag is checked to deter-
mine whether this object has been disposed at the beginning of every method/property/
indexer. If it has been disposed, the
ObjectDisposedException is thrown. This pre-
vents the use of an object after it has been disposed and potentially placed in an
unknown state.
Disposable objects should always check to see if they have been dis-
posed in all of their public methods, properties, and indexers. If a cli-
ent attempts to use your object after it has been disposed, an
ObjectDisposedException should be thrown. Note that a Dispose
method can be called multiple times after this object has been dis-
posed without having any side effects (including the throwing of
ObjectDisposedExceptions) on the object.
Any classes inheriting from Foo need not implement the IDisposable interface; it is
implied from the base class. The inheriting class should implement the
hasBeenDisposed Boolean flag field and use this flag in any methods/properties/index-
ers to confirm that this object has been disposed. Finally, a
Dispose method is imple-
mented that accepts a Boolean flag and overrides the same virtual method in the base
class. This
Dispose method does not have to call the GC.SuppressFinalize(this) ...