Willcode4cash icon

IDisposable class pattern

Willcode4cash | PRO | 08/15/16 03:55:44 AM UTC | 0 ⭐ | 271 👁️ | Never ⏰ | []
C# |

1.21 KB

|

None

|

0 👍

/

0 👎

public class ClassName : IDisposable
{
    #region Variable Declarations
    private bool _disposed = false;
    #endregion
 
    #region Constructors & Destructors
    public ClassName()
    {
    }
 
    ~ClassName()
    {
        //Invoke the helper class that will clean up and release any applicable resources.
        //Note that we are passing a false which indicates that the Garbage Collector 
        //triggered the clean up.
        ReleaseResources(false);
    }
    #endregion
 
    #region IDisposable Interface Methods
    public void Dispose()
    {
        //Invoke the helper class that will clean up and release any applicable resources.
        //Note that we are passing a true which indicates that the method was invoked by
        //the caller/user.
        ReleaseResources(true);
 
        //Suppress the Finalization step of the Garbage Collector.
        GC.SuppressFinalize(this);
    }
 
    private void ReleaseResources(bool disposing)
    {
        //Ensure that the object has not already been disposed.
        if (!this._disposed)
        {
            if (disposing)
            {
                //Clean up and dispose of all MANAGED resources here.
            }
 
            //Clean up and dispose of all UNMANAGED resources here.
        }
 
        //Indicate that the object has already been disposed.
        this._disposed = true;
    }
    #endregion
}

Comments