ReferenceBehavior of every disposable
Behavior of every disposable
Every disposable side by side: whether it is safe to dispose twice, reports IsDisposed, allocates, has an async form and disposes a late resource.
The matrix below and Which disposable? are drawn from one data file, so they agree. Each name links its card on that page.
| Disposable | Safe to dispose twice | Reports IsDisposed | Allocation-free | Sync and async forms | Disposes a late resource | What it holds | What Dispose does |
|---|---|---|---|---|---|---|---|
Disposable.Create | Yes | Yes | No | Yes | Does not apply | An Action, or a Func whose result it discards | Runs the action on the first call; later calls do nothing |
Disposable.CreateRepeatable | No | Yes | No | Yes | Does not apply | An Action | Runs the action on every call |
Disposable.CreateValue | No | No | Yes | Yes | Does not apply | A static Action<TState> and its state | Runs the action on every dispose of every copy |
Disposable.Collection | Yes | Yes | No | Yes | Yes | Any number of resources, in the order you add them | Disposes every item in reverse order, on the first call |
Disposable.Aggregate | Yes | Yes | No | Yes | Yes | One resource, or null | Disposes the resource it holds |
Disposable.Serial | Yes | Yes | No | Yes | Yes | One resource, or null | Disposes the resource it holds |
Disposable.SingleAssignment | Yes | Yes | No | Yes | Yes | One resource, assigned once | Disposes the resource, if one was assigned |
Disposable.Guard | Yes | Yes | No | Yes | Does not apply | One resource, until Release() hands it on | Disposes the resource unless it was released |
SharedReference.Create | No | No | No | Yes | Does not apply | A lease on a reference-counted resource | Releases one reference; the last release disposes the resource |
Disposable.Context | Yes | Yes | No | Yes | Does not apply | One resource and a SynchronizationContext | Disposes the resource once, on the context |
semaphore.Lock() and LockAsync() | No | No | Yes | No | Does not apply | The lock it took | Releases the lock on every dispose |
Disposable.Cancellation | Yes | Yes | No | Yes | Does not apply | A CancellationTokenSource | Cancels its token, then disposes a source it created |
Disposable.Empty | Yes | No | Yes | Yes | Does not apply | Nothing | Nothing |
Disposable.Flag | Yes | Yes | No | No | Does not apply | Nothing but its flag | Sets IsDisposed |
AsyncDisposable.Adapt | Yes | Yes | No | Does not apply | Does not apply | One IDisposable | Disposes the resource once, synchronously |
Reading the matrix
Section titled “Reading the matrix”- Safe to dispose twice: a second
Disposedoes nothing. Once, or on every call explains the types that run their action again. - Reports
IsDisposed: it implementsIDisposalStatus. - Allocation-free: a struct, or one shared instance.
- Sync and async forms:
DisposableandAsyncDisposableboth create it. - Disposes a late resource: a resource handed to it after disposal is disposed at once, and “Does not apply” marks a type that is handed no resource. Disposal status and late resources explains the rule.