Dave Sexton's Blog

on software development using C# and the Microsoft .NET Framework
Welcome to Dave Sexton's Blog Sign in | Join | Help

IDisposable Implementation Snippet

File Details
Downloads: 268 File Size: 1.9kB
Posted By: Dave Sexton Views: 673
Date Added: Mon, Mar 19 2007

My snippets are provided "as is", with no warranty of any kind, either express or implied.  Use at your own risk.

Creates the canonical IDisposable implementation.

Shortcut: disposable

For more information on my C# Code Snippets see my blog entry: Custom C# Code Snippets.  All of my snippets may be downloaded at once here.

Usage

This snippet is intended to be used as expansion only from within the body of a class definition. The class shouldn't derive, directly or indirectly, from a class with a protected virtual void Dispose(bool disposing) method. This snippet should be used on a class that implements IDisposable.

Example Output

The following code illustrates this snippet's output when it's executed for expansion from within the body of the Person class:

class Person : IDisposable
{
  /// <summary>
  /// Releases all resources used by an instance of the <see cref="Person" /> class.
  /// </summary>
  /// <remarks>
  /// This method calls the virtual <see cref="Dispose(bool)" /> method, 
  /// passing in <strong>true</strong>, and then suppresses 
  /// finalization of the instance.
  /// </remarks>
  public void Dispose()
  {
    Dispose(true);
    GC.SuppressFinalize(this);
  }

  /// <summary>
  /// Releases unmanaged resources before an instance of the <see cref="Person" /> 
  /// class is reclaimed by garbage collection.
  /// </summary>
  /// <remarks>
  /// This method releases unmanaged resources by calling the virtual <see cref="Dispose(bool)" /> 
  /// method, passing in <strong>false</strong>.
  /// </remarks>
  ~Person()
  {
    Dispose(false);
  }

  /// <summary>
  /// Releases the unmanaged resources used by an instance of the <see cref="Person" /> 
  /// class and optionally releases the managed resources.
  /// </summary>
  /// <param name="disposing"><strong>true</strong> to release both managed 
  /// and unmanaged resources; <strong>false</strong> to release only unmanaged resources.</param>
  protected virtual void Dispose(bool disposing)
  {
    | {text cursor goes here}
  }
}

Filed under: , ,
Comments
No comments exist for this file.

Add Comment

Name (required)
Web Site (optional)
Comment (required)
Add
Powered by Community Server (Personal Edition), by Telligent Systems