Should IDisposable.Dispose() implementations be idempotent?

前端 未结 4 1252
旧时难觅i
旧时难觅i 2020-12-18 18:28

The Microsoft.NET framework provides the IDisposable interface which requires an implementation of void Dispose() method. Its purpose is to enable

4条回答
  •  一整个雨季
    2020-12-18 18:52

    should the implementation of the Dispose() method be idempotent

    Yes, it should. There is no telling how many times it will be called.

    From Implementing a Dispose Method on MSDN:

    a Dispose method should be callable multiple times without throwing an exception.

    An object with a good implementation of IDispose will have a boolean field flag indicating if it has been disposed of already and on subsequent calls do nothing (as it was already disposed).

提交回复
热议问题