开发者

How does .Dispose() work in C#? [duplicate]

开发者 https://www.devze.com 2023-04-03 05:43 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Proper use of the IDisposable interface
This question already has answers here: Closed 11 years ago.

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号