IDisposable Pattern

 Rate It (1)

The IDisposable pattern isn't one of the a classic patterns. It's a pattern suggested in
MSDN to implement the IDisposable interface. You should be familiar with the pattern
or with the interface because it's a basic thing to know about the .Net framework.

How to Use IDisposable Pattern
Lets look at a code example.

    public class DisposeObject : IDisposable

    {

        #region Members

 

        private bool _disposed = false;

 

        #endregion

 

        #region IDisposable Members

 

        ~DisposeObject()

        {

            Dispose(false);

        }

 

        /// <summary>

        /// Dispose the current object

        /// </summary>

        public void Dispose()

        {

            Dispose(true);

        }

 

        private void Dispose(bool disposing)

        {

            if (!_disposed)

            {

                if (disposing)

                {

                    // clean up resources

                    CleanUp();

 

                    // The object will be cleaned up only if the method

                    // gets true - we are in the Dispose method.

                    // Therefore, you should call GC.SupressFinalize to

                    // take this object off the finalization queue

                    // and prevent finalization code for this object

                    // from executing a second time.

                    // Taken from MSDN.

                    GC.SuppressFinalize(this);

                }

 

                // dispose occurred

                _disposed = true;

            }

        }

 

        private void CleanUp()

        {

            // put here the code to dispose all managed

            // and unmanaged resources

        }

 

        #endregion

    }

Revision number 1, Friday, October 03, 2008 8:23:54 PM by gilfink

Comments

Related Articles

Design Patterns

Design Patterns Design patterns are recognized solutions to common problems defined originally by the Gang of Four programmers. Design patterns are used throughout the ASP.NET Framework. The various patterns are commonly divided into several different groups

Prototype Pattern

The prototype is built upon the use of object cloning. The prototype creates new objects by cloning one of its concrete classes. The prototype is used in the following situations: You need to hide the concrete product classes from the client. You want to reduce

Threat Modeling

It's absolutely necessary if you're serious about security. Whitepapers/Books/Blogs Threat Modeling for ASP.NET (PDF) - an excellent white paper from Rüdiger Grimm and Henrik Eichstädt from the University of Kent Threat Modeling book from MSPress

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (5)
  2. SGWellens (4)
  3. maartenba (2)
  4. rami_nassar (2)
  5. stiansol (2)
  6. MisterFantastic (2)
  7. satish1.v (1)
  8. raklos (1)
  9. mosessaur (1)
  10. Jos Branders (1)

Advertise Here

Microsoft Communities
Page view counter