SHARE
TWEET

Untitled

a guest Jun 21st, 2012 157 Never
  1. public class BetterDisposableClass : IDisposable {
  2.  
  3.   private bool disposed = false;
  4.  
  5.   public void Dispose() {
  6.     if (disposed) return;
  7.     CleanUpManagedResources();
  8.     CleanUpNativeResources();
  9.     GC.SuppressFinalize(this);
  10.     disposed = true;
  11.   }
  12.  
  13.   public void ImportantOperation() {
  14.     if (disposed) throw new ObjectDisposedException();
  15.     // ...
  16.   }
  17.  
  18.   protected virtual void CleanUpManagedResources() {
  19.     // ...
  20.   }
  21.   protected virtual void CleanUpNativeResources() {
  22.     // ...
  23.   }
  24.  
  25.   ~BetterDisposableClass() {
  26.     CleanUpNativeResources();
  27.   }
  28.  
  29. }
RAW Paste Data
Top