Why does the traditional Dispose pattern suppress finalize?

后端 未结 6 1140
渐次进展
渐次进展 2021-01-17 22:10

Assuming this as the traditional Dispose pattern (taken from devx but seen on many websites)

class Test : IDisposable
{
  private bool isDisposed = false;

          


        
6条回答
  •  清歌不尽
    2021-01-17 22:53

    // If the monitor.Dispose method is not called, the example displays the following output:
    //       ConsoleMonitor instance....
    //       The ConsoleMonitor class constructor.
    //       The Write method.
    //       The ConsoleMonitor finalizer.
    //       The Dispose(False) method.
    //       Disposing of unmanaged resources.
    //       
    // If the monitor.Dispose method is called, the example displays the following output:
    //       ConsoleMonitor instance....
    //       The ConsoleMonitor class constructor.
    //       The Write method.
    //       The Dispose method.
    //       The Dispose(True) method.
    //       Disposing of managed resources.
    //       Disposing of unmanaged resources.
    

    From https://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize(v=vs.110).aspx

提交回复
热议问题