DisposableObject 1.0.0

dotnet add package DisposableObject --version 1.0.0                
NuGet\Install-Package DisposableObject -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DisposableObject" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DisposableObject --version 1.0.0                
#r "nuget: DisposableObject, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DisposableObject as a Cake Addin
#addin nuget:?package=DisposableObject&version=1.0.0

// Install DisposableObject as a Cake Tool
#tool nuget:?package=DisposableObject&version=1.0.0                

DisposableObject NuGet

A simple base class for disposable objects.

Usage

This library provides a base class DisposableObject for classes that need to implement the IDisposable Interface.

namespace System
{
    public abstract class DisposableObject : IDisposable
    {
        ~DisposableObject()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!disposed_)
            {
                if (disposing)
                {
                    DisposeManaged();
                }

                DisposeUnmanaged();

                disposed_ = true;
            }
        }

        protected virtual void DisposeManaged()
        {
        }

        protected virtual void DisposeUnmanaged()
        {
        }

        protected void ThrowIfDisposed()
        {
            if (disposed_)
                throw new ObjectDisposedException(string.Format("{0} object is disposed.", GetType().Name));
        }

        protected bool disposed_ = false;
    }
}

As shown above, DisposableObject provides two virtual methods, DisposeManaged and DisposeUnmanaged, to clean up managed and unmanaged resources respectively. You can override one or both of these methods as needed.

The ThrowIfDisposed method checks whether the current object has been disposed, and if so, throws an exception.

Known Issues

  1. Given that C# does not support multiple inheritance of classes, if your class already includes a base class, it may not be possible to inherit from DisposableObject. In such a case, you can use DisposableObject as a reference to implement the IDisposable Interface on your own.
  2. (Feedback is welcome.)

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
.NET Framework net35 is compatible.  net40 was computed.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.
  • .NETFramework 3.5

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DisposableObject:

Package Downloads
SharpEDFlib

.NET binding for Teunis van Beelen's EDFlib.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 752 5/8/2023