Possible Duplicate:
Proper use of the IDisposable interface When is Dispose necessary?
If I write a class, which uses graphics, or threads for example, must I implement the IDisposable interface to write a Dispose() method or else memory leak occurs? Does Dipose() run when the GC reaches it? If I implement a Dispose() method, shall I call Dispose() on all the disposable fields in the class when "dispos开发者_如何学JAVAing" parameter is true, or is it automatic? What is "unmanaged resource"?
So I'm pretty unsure about it, I'd appreciate anything that helps me understand these things :) Thank You
IDisposable
is just a pattern to follow. It doesn't do anything, in and of itself, special (other than fitting the requirements of the using statement).
If you're working with a native resource, the resource isn't "known" to the garbage collector. IDisposable
is the convention used to allow you to free this resource deterministically.
For details, I wrote a detailed series on IDisposable which talks about why and how to implement it for various scenarios (including wrapping a native resource, encapsulating other IDispsable types, etc).
精彩评论