<PackageReference Include="System.Reactive" Version="6.0.1" />

SingleAssignmentDisposable

Represents a disposable resource which only allows a single assignment of its underlying disposable resource. If an underlying disposable resource has already been set, future attempts to set the underlying disposable resource will throw an InvalidOperationException.
using System.Runtime.CompilerServices; namespace System.Reactive.Disposables { public sealed class SingleAssignmentDisposable : ICancelable, IDisposable { private SingleAssignmentDisposableValue _current; public bool IsDisposed => _current.IsDisposed; [System.Runtime.CompilerServices.Nullable(2)] public IDisposable Disposable { [System.Runtime.CompilerServices.NullableContext(2)] get { return _current.Disposable; } [System.Runtime.CompilerServices.NullableContext(2)] set { _current.Disposable = value; } } public void Dispose() { _current.Dispose(); } } }