Dispose Pattern

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HeapStackManaged
{
    /// <summary>
    /// Represents a Dummy class.
    /// </summary>
    public class Dummy : IDisposable
    {
        /// <summary>
        /// Contains definition if this instance is disposed.
        /// </summary>
        private bool _isDisposed;

        /// <summary>
        /// Finalizes this instance.
        /// </summary>
        ~Dummy()
        {
            // Dispose this instance.
            this.Dispose(false);
        }

        /// <summary>
        /// Disposes this instance.
        /// </summary>
        private void Dispose(bool isDisposeByUser)
        {
            if (isDisposeByUser)
            {
                // TODO: Clean up any managed resources.
            }
   
            // TODO: Clean up any unmanaged resources.

            // Suppress execution of the finalizer for this object.
            GC.SuppressFinalize(this);

            // Define this instance as disposed.
            this._isDisposed = true;
        }

        /// <summary>
        /// Dispose this instance.
        /// </summary>
        public void Dispose()
        {
            // If this instance has been disposed, throw an exception.
            if (this._isDisposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }

            // Dispose this instance.
            this.Dispose(true);
        }
    }
}

Schreibe einen Kommentar