Disposable.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.Reactive.Disposables
  5. {
  6. /// <summary>
  7. /// Provides a set of static methods for creating Disposables.
  8. /// </summary>
  9. public static class Disposable
  10. {
  11. /// <summary>
  12. /// Gets the disposable that does nothing when disposed.
  13. /// </summary>
  14. public static IDisposable Empty
  15. {
  16. get { return DefaultDisposable.Instance; }
  17. }
  18. /// <summary>
  19. /// Creates a disposable object that invokes the specified action when disposed.
  20. /// </summary>
  21. /// <param name="dispose">Action to run during the first call to <see cref="IDisposable.Dispose"/>. The action is guaranteed to be run at most once.</param>
  22. /// <returns>The disposable object that runs the given action upon disposal.</returns>
  23. /// <exception cref="ArgumentNullException"><paramref name="dispose"/> is null.</exception>
  24. public static IDisposable Create(Action dispose)
  25. {
  26. if (dispose == null)
  27. throw new ArgumentNullException("dispose");
  28. return new AnonymousDisposable(dispose);
  29. }
  30. }
  31. }