Finalize vs Dispose

2019-01-01 16:36发布

Why do some people use the Finalize method over the Dispose method?

In what situations would you use the Finalize method over the Dispose method and vice versa?

标签: c# dispose
13条回答
美炸的是我
2楼-- · 2019-01-01 17:37

99% of the time, you should not have to worry about either. :) But, if your objects hold references to non-managed resources (window handles, file handles, for example), you need to provide a way for your managed object to release those resources. Finalize gives implicit control over releasing resources. It is called by the garbage collector. Dispose is a way to give explicit control over a release of resources and can be called directly.

There is much much more to learn about the subject of Garbage Collection, but that's a start.

查看更多
登录 后发表回答