Why call dispose(false) in the destructor?

前端 未结 6 1481
挽巷
挽巷 2020-12-12 11:33

What follows is a typical dispose pattern example:

 public bool IsDisposed { get; private set; }

  #region IDisposable Members

  public void Dispose()
  {
         


        
6条回答
  •  温柔的废话
    2020-12-12 12:26

    "The idea here is that Dispose(Boolean) knows whether it is being called to do explicit cleanup (the Boolean is true) versus being called due to a garbage collection (the Boolean is false). This distinction is useful because, when being disposed explicitly, the Dispose(Boolean) method can safely execute code using reference type fields that refer to other objects knowing for sure that these other objects have not been finalized or disposed of yet. When the Boolean is false, the Dispose(Boolean) method should not execute code that refer to reference type fields because those objects may have already been finalized."

    There's much more info in the “Dispose, Finalization, and Resource Management Design Guidelines”.

    Edit: link.

提交回复
热议问题