Nullable.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. using System.Collections.Generic;
  5. using System.Runtime.Versioning;
  6. namespace System
  7. {
  8. // Because we have special type system support that says a boxed Nullable<T>
  9. // can be used where a boxed<T> is use, Nullable<T> can not implement any intefaces
  10. // at all (since T may not). Do NOT add any interfaces to Nullable!
  11. //
  12. [Serializable]
  13. [NonVersionable] // This only applies to field layout
  14. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  15. public partial struct Nullable<T> where T : struct
  16. {
  17. private readonly bool hasValue; // Do not rename (binary serialization)
  18. internal T value; // Do not rename (binary serialization) or make readonly (can be mutated in ToString, etc.)
  19. [NonVersionable]
  20. public Nullable(T value)
  21. {
  22. this.value = value;
  23. hasValue = true;
  24. }
  25. public bool HasValue
  26. {
  27. [NonVersionable]
  28. get => hasValue;
  29. }
  30. public T Value
  31. {
  32. get
  33. {
  34. if (!hasValue)
  35. {
  36. ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue();
  37. }
  38. return value;
  39. }
  40. }
  41. [NonVersionable]
  42. public T GetValueOrDefault() => value;
  43. [NonVersionable]
  44. public T GetValueOrDefault(T defaultValue) =>
  45. hasValue ? value : defaultValue;
  46. public override bool Equals(object? other)
  47. {
  48. if (!hasValue) return other == null;
  49. if (other == null) return false;
  50. return value.Equals(other);
  51. }
  52. public override int GetHashCode() => hasValue ? value.GetHashCode() : 0;
  53. public override string? ToString() => hasValue ? value.ToString() : "";
  54. [NonVersionable]
  55. public static implicit operator Nullable<T>(T value) =>
  56. new Nullable<T>(value);
  57. [NonVersionable]
  58. public static explicit operator T(Nullable<T> value) => value!.Value;
  59. }
  60. public static class Nullable
  61. {
  62. public static int Compare<T>(Nullable<T> n1, Nullable<T> n2) where T : struct
  63. {
  64. if (n1.HasValue)
  65. {
  66. if (n2.HasValue) return Comparer<T>.Default.Compare(n1.value, n2.value);
  67. return 1;
  68. }
  69. if (n2.HasValue) return -1;
  70. return 0;
  71. }
  72. public static bool Equals<T>(Nullable<T> n1, Nullable<T> n2) where T : struct
  73. {
  74. if (n1.HasValue)
  75. {
  76. if (n2.HasValue) return EqualityComparer<T>.Default.Equals(n1.value, n2.value);
  77. return false;
  78. }
  79. if (n2.HasValue) return false;
  80. return true;
  81. }
  82. // If the type provided is not a Nullable Type, return null.
  83. // Otherwise, returns the underlying type of the Nullable type
  84. public static Type? GetUnderlyingType(Type nullableType)
  85. {
  86. if ((object)nullableType == null)
  87. {
  88. throw new ArgumentNullException(nameof(nullableType));
  89. }
  90. #if CORERT
  91. // This is necessary to handle types without reflection metadata
  92. if (nullableType.TryGetEEType(out EETypePtr nullableEEType))
  93. {
  94. if (nullableEEType.IsGeneric)
  95. {
  96. if (nullableEEType.IsNullable)
  97. {
  98. return Internal.Reflection.Core.NonPortable.RuntimeTypeUnifier.GetRuntimeTypeForEEType(nullableEEType.NullableType);
  99. }
  100. }
  101. return null;
  102. }
  103. #endif
  104. if (nullableType.IsGenericType && !nullableType.IsGenericTypeDefinition)
  105. {
  106. // instantiated generic type only
  107. Type genericType = nullableType.GetGenericTypeDefinition();
  108. if (object.ReferenceEquals(genericType, typeof(Nullable<>)))
  109. {
  110. return nullableType.GetGenericArguments()[0];
  111. }
  112. }
  113. return null;
  114. }
  115. }
  116. }