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

SingleAssignmentDisposableValue

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; using System.Threading; namespace System.Reactive.Disposables { [System.Runtime.CompilerServices.NullableContext(2)] [System.Runtime.CompilerServices.Nullable(0)] public struct SingleAssignmentDisposableValue { private IDisposable _current; public bool IsDisposed => Volatile.Read(ref _current) == BooleanDisposable.True; public IDisposable Disposable { get { return System.Reactive.Disposables.Disposable.GetValueOrDefault(ref _current); } set { if (System.Reactive.Disposables.Disposable.TrySetSingle(ref _current, value) == TrySetSingleResult.AlreadyAssigned) throw new InvalidOperationException(Strings_Core.DISPOSABLE_ALREADY_ASSIGNED); } } public void Dispose() { System.Reactive.Disposables.Disposable.Dispose(ref _current); } public override bool Equals(object obj) { return false; } public override int GetHashCode() { return 0; } public static bool operator ==(SingleAssignmentDisposableValue left, SingleAssignmentDisposableValue right) { return false; } public static bool operator !=(SingleAssignmentDisposableValue left, SingleAssignmentDisposableValue right) { return true; } } }