BooleanDisposable.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. #nullable disable
  5. namespace System.Reactive.Disposables
  6. {
  7. /// <summary>
  8. /// Represents a disposable resource that can be checked for disposal status.
  9. /// </summary>
  10. public sealed class BooleanDisposable : ICancelable
  11. {
  12. // Keep internal! This is used as sentinel in other IDisposable implementations to detect disposal and
  13. // should never be exposed to user code in order to prevent users from swapping in the sentinel. Have
  14. // a look at the code in e.g. SingleAssignmentDisposable for usage patterns.
  15. internal static readonly BooleanDisposable True = new BooleanDisposable(true);
  16. private volatile bool _isDisposed;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="BooleanDisposable"/> class.
  19. /// </summary>
  20. public BooleanDisposable()
  21. {
  22. }
  23. private BooleanDisposable(bool isDisposed)
  24. {
  25. _isDisposed = isDisposed;
  26. }
  27. /// <summary>
  28. /// Gets a value that indicates whether the object is disposed.
  29. /// </summary>
  30. public bool IsDisposed => _isDisposed;
  31. /// <summary>
  32. /// Sets the status to disposed, which can be observer through the <see cref="IsDisposed"/> property.
  33. /// </summary>
  34. public void Dispose()
  35. {
  36. _isDisposed = true;
  37. }
  38. }
  39. }