How does the IDisposable interface work?

前端 未结 4 1744
谎友^
谎友^ 2021-01-17 20:01

I understand that it is used to deallocate unmanaged resources, however, I am confused as to when Dispose is actually called. I know it is called at the end of

4条回答
  •  遇见更好的自我
    2021-01-17 20:19

    Dispose is called in a few places:

    1. At the end of a using block.
    2. When explicitly called (in a try{} finally{} for instance.)

    It is recommended that you call it yourself when you are done with a resource, to better manage resources.

    EDIT: I was mistaken. Dispose is NOT called during garbage collection. See this article.

提交回复
热议问题