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

SerialDisposableValue

Represents a disposable resource whose underlying disposable resource can be replaced by another disposable resource, causing automatic disposal of the previous underlying disposable resource.
using System.Runtime.CompilerServices; using System.Threading; namespace System.Reactive.Disposables { [System.Runtime.CompilerServices.NullableContext(2)] [System.Runtime.CompilerServices.Nullable(0)] internal struct SerialDisposableValue : ICancelable, IDisposable { private IDisposable _current; public bool IsDisposed => Volatile.Read(ref _current) == BooleanDisposable.True; public IDisposable Disposable { get { return System.Reactive.Disposables.Disposable.GetValue(ref _current); } set { System.Reactive.Disposables.Disposable.TrySetSerial(ref _current, value); } } [System.Runtime.CompilerServices.NullableContext(1)] public bool TrySetFirst(IDisposable disposable) { return System.Reactive.Disposables.Disposable.TrySetSingle(ref _current, disposable) == TrySetSingleResult.Success; } public void Dispose() { System.Reactive.Disposables.Disposable.Dispose(ref _current); } } }