This is the first post in a series of posts in which I will publish some C# code snippets I find useful in everyday C# coding.

This snippets inserts all of the necessary code to implement Disposable pattern in a C# class. The code is basically the example of how to implement IDisposable interface taken from the FxCop help pages, so this snippet resolves that FxCop issue.

Note the two “TODO”-s where you can add your code for disposing of managed and unmanaged resources.

<codesnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <codesnippet format="1.0.0">

            <description>Implements a disposable pattern for the class.</description>
            <author>Igor Brejc</author>
            <keywords>
                <keyword>IDisposable</keyword>
                <keyword>Dispose</keyword>
            </keywords>
            <snippettypes>
                <snippettype>Expansion</snippettype>
            </snippettypes>

        <snippet>
            <code language="CSharp" kind="method decl">
                <!–[CDATA[
      #region IDisposable Members

      /// <summary>

      /// Performs application-defined tasks associated with freeing, releasing, or
      /// resetting unmanaged resources.
      /// </summary>
      public void Dispose()
      {
         Dispose(true);
         GC.SuppressFinalize(this);
      }

      /// <summary>
      /// Disposes the object.
      /// </summary>
      ///
<div  name="disposing"></div>
If <code>false</code>, cleans up native resources.
      /// If <code>true</code> cleans up both managed and native resources</param>
      protected virtual void Dispose(bool disposing)
      {
            if (false == disposed)
            {
                // TODO: clean native resources        

             if (disposing)
             {
                    // TODO: clean managed resources
                    $end$
             }

             disposed = true;
            }
      }

      private bool disposed;

      #endregion
                ]]–>
            </code>
        </snippet>
    </codesnippet>
</codesnippets>