Unit.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. namespace System.Reactive
  5. {
  6. /// <summary>
  7. /// Represents a type with a single value. This type is often used to denote the successful completion of a void-returning method (C#) or a Sub procedure (Visual Basic).
  8. /// </summary>
  9. [Serializable]
  10. public readonly struct Unit : IEquatable<Unit>
  11. {
  12. /// <summary>
  13. /// Determines whether the specified <see cref="Unit"/> value is equal to the current <see cref="Unit"/>. Because <see cref="Unit"/> has a single value, this always returns <c>true</c>.
  14. /// </summary>
  15. /// <param name="other">An object to compare to the current <see cref="Unit"/> value.</param>
  16. /// <returns>Because <see cref="Unit"/> has a single value, this always returns <c>true</c>.</returns>
  17. public bool Equals(Unit other) => true;
  18. /// <summary>
  19. /// Determines whether the specified System.Object is equal to the current <see cref="Unit"/>.
  20. /// </summary>
  21. /// <param name="obj">The System.Object to compare with the current <see cref="Unit"/>.</param>
  22. /// <returns><c>true</c> if the specified System.Object is a <see cref="Unit"/> value; otherwise, <c>false</c>.</returns>
  23. public override bool Equals(object? obj) => obj is Unit;
  24. /// <summary>
  25. /// Returns the hash code for the current <see cref="Unit"/> value.
  26. /// </summary>
  27. /// <returns>A hash code for the current <see cref="Unit"/> value.</returns>
  28. public override int GetHashCode() => 0;
  29. /// <summary>
  30. /// Returns a string representation of the current <see cref="Unit"/> value.
  31. /// </summary>
  32. /// <returns>String representation of the current <see cref="Unit"/> value.</returns>
  33. public override string ToString() => "()";
  34. /// <summary>
  35. /// Determines whether the two specified <see cref="Unit"/> values are equal. Because <see cref="Unit"/> has a single value, this always returns <c>true</c>.
  36. /// </summary>
  37. /// <param name="first">The first <see cref="Unit"/> value to compare.</param>
  38. /// <param name="second">The second <see cref="Unit"/> value to compare.</param>
  39. /// <returns>Because <see cref="Unit"/> has a single value, this always returns <c>true</c>.</returns>
  40. [Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "first", Justification = "Parameter required for operator overloading."), Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "second", Justification = "Parameter required for operator overloading.")]
  41. public static bool operator ==(Unit first, Unit second) => true;
  42. /// <summary>
  43. /// Determines whether the two specified <see cref="Unit"/> values are not equal. Because <see cref="Unit"/> has a single value, this always returns <c>false</c>.
  44. /// </summary>
  45. /// <param name="first">The first <see cref="Unit"/> value to compare.</param>
  46. /// <param name="second">The second <see cref="Unit"/> value to compare.</param>
  47. /// <returns>Because <see cref="Unit"/> has a single value, this always returns <c>false</c>.</returns>
  48. [Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "first", Justification = "Parameter required for operator overloading."), Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "second", Justification = "Parameter required for operator overloading.")]
  49. public static bool operator !=(Unit first, Unit second) => false;
  50. /// <summary>
  51. /// Gets the single <see cref="Unit"/> value.
  52. /// </summary>
  53. public static Unit Default => default;
  54. }
  55. }