https://github.com/sharpjs/sharp.disposable
Thread-safe implementation of the .NET Dispose Pattern, plus extras.
https://github.com/sharpjs/sharp.disposable
Last synced: 10 months ago
JSON representation
Thread-safe implementation of the .NET Dispose Pattern, plus extras.
- Host: GitHub
- URL: https://github.com/sharpjs/sharp.disposable
- Owner: sharpjs
- License: isc
- Created: 2018-07-24T15:04:12.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2022-08-11T16:20:39.000Z (over 3 years ago)
- Last Synced: 2025-02-26T20:39:05.833Z (11 months ago)
- Language: C#
- Homepage:
- Size: 242 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Sharp.Disposable
Thread-safe implementation of the [.NET Dispose Pattern](https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose), plus extras.
## Status
[](https://github.com/sharpjs/Sharp.Disposable/actions)
[](https://www.nuget.org/packages/Sharp.Disposable)
[](https://www.nuget.org/packages/Sharp.Disposable)
- **Stable:** in production for years with no reported defects.
- **Tested:** 100% coverage by automated tests.
- **Documented:** IntelliSense on everything.
## Overview
This package provides the following types in the `Sharp.Disposable` namespace:
Name | Description
-----------------|------------
`Disposable` | Base class for any disposable object.
– Thread-safe; dispose from any thread.
– Guarantees that disposal happens only once.
– Provides an `IsDisposed` property.
– Provides a `RequireNotDisposed()` helper method.
`DisposableBox` | Generic, mutable box that can hold a single disposable object.
– The object can be *owned* or *borrowed*.
– Disposes an owned object when another is placed in the box.
– Disposes an owned object when the box itself is disposed.
`DisposablePool` | Collects multiple disposable objects, disposing them when the pool itself is disposed.
`Finalizer` | Methods to force object finalization.
## Usage
```csharp
using Sharp.Disposable;
public class Foo : Disposable
{
private SomeDisposable _bar; // a managed resource
// ...
protected override bool Dispose(bool managed)
{
// Check if already disposed
if (!base.Dispose(managed))
// False means nothing happened because already disposed
return false;
// Clean up unmanaged resources (like temp files) here
DeleteTemporaryFiles();
// Check if doing managed disposal too
if (managed)
// Disposed managed resources (other IDisposables) here
_bar.Dispose();
// True means disposal happened
return true;
}
}
```