DisposableObject 1.0.0
dotnet add package DisposableObject --version 1.0.0
NuGet\Install-Package DisposableObject -Version 1.0.0
<PackageReference Include="DisposableObject" Version="1.0.0" />
paket add DisposableObject --version 1.0.0
#r "nuget: DisposableObject, 1.0.0"
// 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
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
- 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 useDisposableObject
as a reference to implement the IDisposable Interface on your own. - (Feedback is welcome.)
Related Projects
License
MIT
Product | Versions 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. |
-
.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 | 869 | 5/8/2023 |