MrMistreater

IDisposable class

May 20th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. public class ClassName : IDisposable
  2. {
  3.     #region Variable Declarations
  4.  
  5.     private bool _disposed = false;
  6.  
  7.     #endregion
  8.  
  9.  
  10.  
  11.     #region Constructors & Destructors
  12.  
  13.     public ClassName()
  14.     {
  15.     }
  16.  
  17.  
  18.  
  19.     ~ClassName()
  20.     {
  21.         //Invoke the helper class that will clean up and release any applicable resources.
  22.         //Note that we are passing a false which indicates that the Garbage Collector
  23.         //triggered the clean up.
  24.         ReleaseResources(false);
  25.     }
  26.  
  27.     #endregion
  28.  
  29.  
  30.  
  31.     #region IDisposable Interface Methods
  32.  
  33.     public void Dispose()
  34.     {
  35.         //Invoke the helper class that will clean up and release any applicable resources.
  36.         //Note that we are passing a true which indicates that the method was invoked by
  37.         //the caller/user.
  38.         ReleaseResources(true);
  39.  
  40.  
  41.  
  42.         //Suppress the Finalization step of the Garbage Collector.
  43.         GC.SuppressFinalize(this);
  44.     }
  45.  
  46.  
  47.  
  48.     private void ReleaseResources(bool disposing)
  49.     {
  50.         //Ensure that the object has not already been disposed.
  51.         if (!this._disposed)
  52.         {
  53.             if (disposing)
  54.             {
  55.                 //Clean up and dispose of all MANAGED resources here.
  56.             }
  57.  
  58.  
  59.  
  60.             //Clean up and dispose of all UNMANAGED resources here.
  61.         }
  62.  
  63.  
  64.  
  65.         //Indicate that the object has already been disposed.
  66.         this._disposed = true;
  67.     }
  68.  
  69.     #endregion
  70. }
Advertisement
Add Comment
Please, Sign In to add comment