Varangian said:
[...]
there are so many sources about how to use the IDisposable.... that's
what i thought that an image is an unmanaged resource.
The classes derived from Image do have unmanaged resources. That's why
you should call Dispose() on them when you're done with them.
But you asked about disposing _managed_ resources. As both Laura and I
have mentioned, managed resources are already managed. You should not
usually need to do anything explicit with them in the way of disposal.
so how the GC.SuppressFinalize comes into use? I read that this may
improve slightly insignificantly the performance... so that managed
object doesn't need to be finalized again because of its managed
resource. how would the SuppressFinalize come into use?
As Laura mentioned, SuppressFinalize() is called by the managed object
when it is disposed, so that the finalizing code knows that it doesn't
need to run the class's finalizer when cleaning up the managed object.
Without calling SuppressFinalize(), a managed object will be finalized
as part of the garbage collection behavior. This is so that classes
that have unmanaged resources to be disposed get an opportunity to do so
even if the code that created the instance of that class did not call
Dispose().
Since the point is to catch the cases where some code erroneously fails
to call Dispose(), it is helpful to have a way to signal to the garbage
collector that Dispose() _has_ been called (or for whatever reason,
finalizing isn't required, though since finalizing is related to
IDisposable being implemented it seems to me that the questions of
whether Dispose() has been called and whether the finalizer should be
called go hand-in-hand). The SuppressFinalize() method is the way this
is done.
So, calling SuppressFinalize() is something that the class implementing
IDisposable does, in the Dispose() method. That way it doesn't have to
be unnecessarily disposed again by the finalizing logic.
Pete