Nullable.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 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
  29. {
  30. return hasValue;
  31. }
  32. }
  33. public T Value
  34. {
  35. get
  36. {
  37. if (!hasValue)
  38. {
  39. ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue();
  40. }
  41. return value;
  42. }
  43. }
  44. [NonVersionable]
  45. public T GetValueOrDefault()
  46. {
  47. return value;
  48. }
  49. [NonVersionable]
  50. public T GetValueOrDefault(T defaultValue)
  51. {
  52. return hasValue ? value : defaultValue;
  53. }
  54. public override bool Equals(object other)
  55. {
  56. if (!hasValue) return other == null;
  57. if (other == null) return false;
  58. return value.Equals(other);
  59. }
  60. public override int GetHashCode()
  61. {
  62. return hasValue ? value.GetHashCode() : 0;
  63. }
  64. public override string ToString()
  65. {
  66. return hasValue ? value.ToString() : "";
  67. }
  68. [NonVersionable]
  69. public static implicit operator Nullable<T>(T value)
  70. {
  71. return new Nullable<T>(value);
  72. }
  73. [NonVersionable]
  74. public static explicit operator T(Nullable<T> value)
  75. {
  76. return value.Value;
  77. }
  78. }
  79. public static class Nullable
  80. {
  81. public static int Compare<T>(Nullable<T> n1, Nullable<T> n2) where T : struct
  82. {
  83. if (n1.HasValue)
  84. {
  85. if (n2.HasValue) return Comparer<T>.Default.Compare(n1.value, n2.value);
  86. return 1;
  87. }
  88. if (n2.HasValue) return -1;
  89. return 0;
  90. }
  91. public static bool Equals<T>(Nullable<T> n1, Nullable<T> n2) where T : struct
  92. {
  93. if (n1.HasValue)
  94. {
  95. if (n2.HasValue) return EqualityComparer<T>.Default.Equals(n1.value, n2.value);
  96. return false;
  97. }
  98. if (n2.HasValue) return false;
  99. return true;
  100. }
  101. // If the type provided is not a Nullable Type, return null.
  102. // Otherwise, returns the underlying type of the Nullable type
  103. public static Type GetUnderlyingType(Type nullableType)
  104. {
  105. if ((object)nullableType == null)
  106. {
  107. throw new ArgumentNullException(nameof(nullableType));
  108. }
  109. #if CORERT
  110. // This is necessary to handle types without reflection metadata
  111. if (nullableType.TryGetEEType(out EETypePtr nullableEEType))
  112. {
  113. if (nullableEEType.IsGeneric)
  114. {
  115. if (nullableEEType.IsNullable)
  116. {
  117. return Internal.Reflection.Core.NonPortable.RuntimeTypeUnifier.GetRuntimeTypeForEEType(nullableEEType.NullableType);
  118. }
  119. }
  120. return null;
  121. }
  122. #endif
  123. if (nullableType.IsGenericType && !nullableType.IsGenericTypeDefinition)
  124. {
  125. // instantiated generic type only
  126. Type genericType = nullableType.GetGenericTypeDefinition();
  127. if (object.ReferenceEquals(genericType, typeof(Nullable<>)))
  128. {
  129. return nullableType.GetGenericArguments()[0];
  130. }
  131. }
  132. return null;
  133. }
  134. }
  135. }