// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT License. // See the LICENSE file in the project root for more information. using System.Threading; namespace System.Reactive.Disposables { /// /// Provides a set of static methods for creating objects. /// public static partial class Disposable { /// /// Represents a disposable that does nothing on disposal. /// private sealed class EmptyDisposable : IDisposable { /// /// Singleton default disposable. /// public static readonly EmptyDisposable Instance = new(); private EmptyDisposable() { } /// /// Does nothing. /// public void Dispose() { // no op } } /// /// Gets the disposable that does nothing when disposed. /// public static IDisposable Empty => EmptyDisposable.Instance; /// /// Creates a disposable object that invokes the specified action when disposed. /// /// Action to run during the first call to . The action is guaranteed to be run at most once. /// The disposable object that runs the given action upon disposal. /// is null. public static IDisposable Create(Action dispose) { if (dispose == null) { throw new ArgumentNullException(nameof(dispose)); } return new AnonymousDisposable(dispose); } /// /// Creates a disposable object that invokes the specified action when disposed. /// /// The state to be passed to the action. /// Action to run during the first call to . The action is guaranteed to be run at most once. /// The disposable object that runs the given action upon disposal. /// is null. public static IDisposable Create(TState state, Action dispose) { if (dispose == null) { throw new ArgumentNullException(nameof(dispose)); } return new AnonymousDisposable(state, dispose); } } }