Hi Frank,
The resources Chris and Jon have pointed are good sources.
I'll just try to explain it in simple words.
You may, or you may not use IDisposable interface, and implement same
disposing mechanism.
The advantage if implementing of IDisposable is, that a lot of
collection classes check if the member of the collection is implementing
IDisposable and invokes the Dispose() method.
And, the purpose of the second implementation with the bool parameter is
only to signal who is calling the dispose process - user code, or
Garbage Collector. But for it to work, you need to implement finalizer
(destructor) which calls Dispose(false), so your example have to be
extended with:
~MyClass()
{
Dispose(false);
}
And, you need to signal who is calling the Dispose(bool) method, because
if it is called by the finalizer (I.e. Grabage Collector), you can not
be sure that the managed resources, referred by your class are still
alive, and you can not dispose/free them. You can free only unmanaged
resources.
Hope that helps
Sunny
P.S. Chris, Jon, please excuse me for these my interventions, but I
personally had a lot of problems understanding some terms, because of my
poor English and lack of term knowledge. Thats why I'm trying to expose
the things as I have understand them, and to explain with simple words.
So please, if somewhere (not only in this post), you find me incorrect,
or misleading, be so kind to correct me. Even if it is "technically"
correct, but completely useless :). Thanks. Sunny
In article <uw************@tk2msftngp13.phx.gbl>,
fr*******@acadx.com
says...
I see a lot of this:
public void Dispose()
{
this.Dispose(true);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
yada.yada();
GC.SupressFinalize(this);
}
}
My question is what's with the two implementations? Should you do this
all the time or only some of the time? If only some of the time, when?
What happens if disposing is false? Do you call identical code
elsewhere?
While we're here, when's the right time to code a Finalize method?