C# disposing IDisposable

后端 未结 7 2104
滥情空心
滥情空心 2021-01-14 02:44

Could someone explain what might happen if you don\'t Dispose some IDisposable entity (by using or direct Dispose cal

7条回答
  •  别那么骄傲
    2021-01-14 03:18

    Some objects will, during their existence, do things to outside entities that they are expected to clean up. There is a very strong convention that such objects should whenever practical implement IDisposable and perform any required cleanup when the IDisposable.Dispose() method is called. The system allows objects to request notification if it notices that they've been abandoned, but there's no guarantee that such notifications will happen in time to be useful. Further, unless one is careful, it's possible for such notifications to fire early and try to clean up things that are still in use.

    Unless you know that a particular class of IDisposable object may be safely abandoned, don't. Always clean up after yourself whenever practical. "Finalization" (the process of notifying objects they've been abandoned) is dangerous and can create many Heisenbugs. Avoid relying on it if any practical alternative exists.

提交回复
热议问题