dispose c#

  • 98
  • 0

dispose c#

// Flag: Has Dispose already been called?

   // Instantiate a SafeHandle instance.
   SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);//修改我
   
   // Public implementation of Dispose pattern callable by consumers.
   public void Dispose()
   { 
      Dispose(true);
      GC.SuppressFinalize(this);           
   }
   
   bool disposed = false;
   // Protected implementation of Dispose pattern.
   protected virtual void Dispose(bool disposing)
   {
      if (disposed)
         return; 
      
      if (disposing) {
         handle.Dispose();//修改我
         // Free any other managed objects here.
         //
      }
      
      disposed = true;
   }

https://docs.microsoft.com/zh-tw/dotnet/standard/garbage-collection/implementing-dispose#dispose-and-disposeboolean