Advertisement
Willcode4cash

IDisposable class pattern

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