Fix pragma warning restore (dotnet/coreclr#26389)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / Nullable.cs
blob77bb353fe8710175facc8fe4097de4bf63521a06
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.
5 using System.Collections.Generic;
6 using System.Runtime.Versioning;
8 namespace System
10 // Because we have special type system support that says a boxed Nullable<T>
11 // can be used where a boxed<T> is use, Nullable<T> can not implement any intefaces
12 // at all (since T may not). Do NOT add any interfaces to Nullable!
14 [Serializable]
15 [NonVersionable] // This only applies to field layout
16 [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
17 public partial struct Nullable<T> where T : struct
19 private readonly bool hasValue; // Do not rename (binary serialization)
20 internal T value; // Do not rename (binary serialization) or make readonly (can be mutated in ToString, etc.)
22 [NonVersionable]
23 public Nullable(T value)
25 this.value = value;
26 hasValue = true;
29 public bool HasValue
31 [NonVersionable]
32 get
34 return hasValue;
38 public T Value
40 get
42 if (!hasValue)
44 ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue();
46 return value;
50 [NonVersionable]
51 public T GetValueOrDefault()
53 return value;
56 [NonVersionable]
57 public T GetValueOrDefault(T defaultValue)
59 return hasValue ? value : defaultValue;
62 public override bool Equals(object? other)
64 if (!hasValue) return other == null;
65 if (other == null) return false;
66 return value.Equals(other);
69 public override int GetHashCode()
71 return hasValue ? value.GetHashCode() : 0;
74 public override string? ToString()
76 return hasValue ? value.ToString() : "";
79 [NonVersionable]
80 public static implicit operator Nullable<T>(T value)
82 return new Nullable<T>(value);
85 [NonVersionable]
86 public static explicit operator T(Nullable<T> value)
88 return value!.Value;
92 public static class Nullable
94 public static int Compare<T>(Nullable<T> n1, Nullable<T> n2) where T : struct
96 if (n1.HasValue)
98 if (n2.HasValue) return Comparer<T>.Default.Compare(n1.value, n2.value);
99 return 1;
101 if (n2.HasValue) return -1;
102 return 0;
105 public static bool Equals<T>(Nullable<T> n1, Nullable<T> n2) where T : struct
107 if (n1.HasValue)
109 if (n2.HasValue) return EqualityComparer<T>.Default.Equals(n1.value, n2.value);
110 return false;
112 if (n2.HasValue) return false;
113 return true;
116 // If the type provided is not a Nullable Type, return null.
117 // Otherwise, returns the underlying type of the Nullable type
118 public static Type? GetUnderlyingType(Type nullableType)
120 if ((object)nullableType == null)
122 throw new ArgumentNullException(nameof(nullableType));
125 #if CORERT
126 // This is necessary to handle types without reflection metadata
127 if (nullableType.TryGetEEType(out EETypePtr nullableEEType))
129 if (nullableEEType.IsGeneric)
131 if (nullableEEType.IsNullable)
133 return Internal.Reflection.Core.NonPortable.RuntimeTypeUnifier.GetRuntimeTypeForEEType(nullableEEType.NullableType);
136 return null;
138 #endif
140 if (nullableType.IsGenericType && !nullableType.IsGenericTypeDefinition)
142 // instantiated generic type only
143 Type genericType = nullableType.GetGenericTypeDefinition();
144 if (object.ReferenceEquals(genericType, typeof(Nullable<>)))
146 return nullableType.GetGenericArguments()[0];
149 return null;