Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: C#  |  size: 0.58 KB  |  views: 88  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }