Should IDisposable.Dispose() implementations be idempotent?

前端 未结 4 1256
旧时难觅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 19:18

    Yes, also make sure the other methods of the class respond correctly when they are called when the object has already been disposed.

    public void SomeMethod()
    {
         if(_disposed)
         {
             throw new ObjectDisposedException();
         }
         else
         {
             // ...
         }
    
    }
    

提交回复
热议问题