Home
Manage Your Code
Snippet: Dispose pattern (with comments and try{}finally block) (C#)
Title: Dispose pattern (with comments and try{}finally block) Language: C#
Description: Implimentation of the IDisposable pattern with try{}finally block and comments. Views: 65
Author: Tim Detering Date Added: 7/15/2008
Copy Code  
1        ~THISCLASSNAME()
2        {
3            this.Dispose( false );
4        }
5
6        //-------------------------------------------------------------------

7        /// <summary>

8        /// Immediately releases the unmanaged resources used by this object.

9        /// </summary>

10        //-------------------------------------------------------------------

11        public void Dispose()
12        {
13            this.Dispose( true );
14            GC.SuppressFinalize( this );
15        }
16
17        //-------------------------------------------------------------------

18        /// <summary>

19        /// Releases the unmanaged resources being used and optionally releases managed resources.

20        /// </summary>

21        /// <param name="disposeManaged">

22        /// <b>true</b> to release both managed and unmanaged resources; <b>false</b> to release only unmanaged resources.

23        /// </param>

24        /// <remarks>

25        /// This method is called by the public Dispose method and the Finalize method. Dispose invokes the protected Dispose(Boolean) method with the <i>disposeManaged</i> parameter set to true. Finalize invokes Dispose(Boolean) with <i>disposeManaged</i> set to false. 

26        /// </remarks>

27        /// <seealso href="http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx"/>

28        //-------------------------------------------------------------------

29        protected virtual void Dispose( bool disposeManaged )
30        {
31            try
32            {
33                if( true == disposeManaged )
34                {
35                    //

36                    //  TODO: Dispose managed resources.

37                    //

38                }
39
40                //

41                //  TODO: Release unmanaged resources.

42                //

43            }
44            finally
45            {
46                //base.Dispose( disposeManaged );

47                this._disposed = true;
48            }
49        }
50
51        private bool            _disposed;