IDisposable, does it really matter

后端 未结 9 3012
予麋鹿
予麋鹿 2021-02-20 09:19

Coming from C/C++ a long time ago I still have a habit of ensuring that all resources are cleaned up correctly. I always ensure Dispose is called on IDisposable classes and impl

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-20 09:48

    Yes it matters. When an object implements IDisposable it is explicitly stating that it is holding resources that need to be released when the object is no longer needed.

    Most will still clean up their resources when the object is finalized but finalization is not deterministic and can't be relied on for resource management.

    Simply wrapping the variable declarations in a using(...) block makes it easy to dispose of properly.

提交回复
热议问题