System.DisposableObject

Base object for disposing managed and unmanaged objects. Supports async disposing with IAsyncDisposable as well as the standard IDisposable.


Keywords
IAsyncDisposable, IDisposable, async, disposable, dispose, disposing-managed, nuget, unmanaged-objects
License
Other
Install
Install-Package System.DisposableObject -Version 8.0.0

Documentation

Nuget Nuget

System.DisposableObject

Base object for disposing managed and unmanaged objects. This object implements the dispose pattern for the .NET Framework.

Example

Create a class and inherit from DisposableObject as shown below:

using System;

public class SomeObject : DisposableObject
{
  protected override void OnDisposeManagedObjects()
  {
    //
    // Disposed CLR managed objects here.
    //
  }

  protected override void OnDisposeUnmanagedObjects()
  {
    //
    // Disposed non-CLR managed objects here.
    //
  }
}

Override one or both of the above methods depending on the type of resources your class uses.

When instantiating the class. wrap it in a using statement.

using (SomeObject obj = new SomeObject())
{
  //
  // obj will be disposed and the dispose
  // methods will be called.
}

or call the Dispose() method.

SomeObject obj = new SomeObject();
obj.Dispose();

In either case, the OnDisposeManagedObjects() and OnDisposeUnmanagedObjects() are called.

References

  1. https://docs.microsoft.com/en-us/dotnet/api/system.idisposable

  2. https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose