You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: nanoFramework.CoreLibrary/System/Nullable.cs
+62-2Lines changed: 62 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
#pragma warning disable CA1066// Implement IEquatable when overriding Object.Equals
5
+
#nullable enable
5
6
6
7
namespaceSystem
7
8
{
@@ -14,26 +15,46 @@ namespace System
14
15
/// <summary>
15
16
/// Represents a value type that can be assigned <see langword="null"/>.
16
17
/// </summary>
17
-
/// <typeparam name="T"></typeparam>
18
+
/// <typeparam name="T">The underlying value type of the <see cref="Nullable{T}"/> generic type.</typeparam>
18
19
[Serializable]
19
20
publicpartialstructNullable<T>whereT:struct
20
21
{
21
22
// Do not rename (binary serialization)
22
23
privatereadonlyboolhasValue;
23
24
// Do not rename (binary serialization) or make readonly (can be mutated in ToString, etc.)
24
-
internalTvalue;
25
+
internalTvalue;
25
26
27
+
/// <summary>
28
+
/// Initializes a new instance of the <see cref="Nullable{T}"/> structure to the specified value.
29
+
/// </summary>
30
+
/// <param name="value">A value type.</param>
31
+
/// <remarks>
32
+
/// The <see cref="Nullable{T}"/> constructor initializes the HasValue property of the new <see cref="Nullable{T}"/> object to <see langword="true"/>, and the Value property to the value of the <paramref name="value"/> parameter.
33
+
/// </remarks>
26
34
publicNullable(Tvalue)
27
35
{
28
36
this.value=value;
29
37
hasValue=true;
30
38
}
31
39
40
+
/// <summary>
41
+
/// Gets a value indicating whether the current <see cref="Nullable{T}"/> object has a valid value of its underlying type.
42
+
/// </summary>
43
+
/// <returns>
44
+
/// <see langword="true"/> if the current <see cref="Nullable{T}"/> object has a value; <see langword="false"/> if the current <see cref="Nullable{T}"/> object has no <see cref="value"/>.
45
+
/// </returns>
32
46
publicreadonlyboolHasValue
33
47
{
34
48
get=>hasValue;
35
49
}
36
50
51
+
/// <summary>
52
+
/// Gets the value of the current <see cref="Nullable{T}"/> object if it has been assigned a valid underlying value.
53
+
/// </summary>
54
+
/// <returns>
55
+
/// The value of the current <see cref="Nullable{T}"/> object if the <see cref="HasValue"/> property is <see langword="true"/>. An exception is thrown if the <see cref="HasValue"/> property is <see langword="false"/>.
56
+
/// </returns>
57
+
/// <exception cref="InvalidOperationException">The <see cref="HasValue"/> property is <see langword="false"/>.</exception>
37
58
publicreadonlyTValue
38
59
{
39
60
get
@@ -46,11 +67,27 @@ public readonly T Value
46
67
}
47
68
}
48
69
70
+
/// <summary>
71
+
/// Retrieves the value of the current <see cref="Nullable{T}"/> object, or the default value of the underlying type.
72
+
/// </summary>
73
+
/// <returns>The value of the <see cref="Value"/> property if the <see cref="HasValue"/> property is <see langword="true"/>; otherwise, the default value of the underlying type.</returns>
74
+
/// <remarks>
75
+
/// The <see cref="GetValueOrDefault()"/> method returns a value even if the <see cref="HasValue"/> property is <see langword="false"/> (unlike the <see cref="Value"/> property, which throws an exception). If the <see cref="HasValue"/> property is <see langword="false"/>, the method returns the default value of the underlying type.
76
+
/// </remarks>
49
77
publicreadonlyTGetValueOrDefault()=>value;
50
78
79
+
/// <summary>
80
+
/// Retrieves the value of the current <see cref="Nullable{T}"/> object, or the specified default value.
81
+
/// </summary>
82
+
/// <param name="defaultValue">A value to return if the <see cref="HasValue"/> property is <see langword="false"/>.</param>
83
+
/// <returns>The value of the <see cref="Value"/> property if the <see cref="HasValue"/> property is <see langword="true"/>; otherwise, the default value of the underlying type.</returns>
84
+
/// <remarks>
85
+
/// The <see cref="GetValueOrDefault(T)"/> method returns a value even if the <see cref="HasValue"/> property is <see langword="false"/> (unlike the <see cref="Value"/> property, which throws an exception). If the <see cref="HasValue"/> property is <see langword="false"/>, the method returns the default value of the underlying type.
86
+
/// </remarks>
51
87
publicreadonlyTGetValueOrDefault(TdefaultValue)=>
52
88
hasValue?value:defaultValue;
53
89
90
+
/// <inheritdoc />
54
91
publicoverrideboolEquals(object?other)
55
92
{
56
93
if(!hasValue)
@@ -66,16 +103,31 @@ public override bool Equals(object? other)
/// Supports a value type that can be assigned <see langword="null"/>. This class cannot be inherited.
130
+
/// </summary>
79
131
publicstaticclassNullable
80
132
{
81
133
//public static int Compare<T>(T? n1, T? n2) where T : struct
@@ -100,6 +152,12 @@ public static class Nullable
100
152
// return true;
101
153
//}
102
154
155
+
/// <summary>
156
+
/// Returns the underlying type argument of the specified nullable type.
157
+
/// </summary>
158
+
/// <param name="nullableType">A <see cref="Type"/> object that describes a closed generic nullable type.</param>
159
+
/// <returns>The type argument of the <paramref name="nullableType"/> parameter, if the <paramref name="nullableType"/> parameter is a closed generic nullable type; otherwise, <see langword="null"/>.</returns>
160
+
/// <exception cref="ArgumentNullException"><paramref name="nullableType"/> is <see langword="null"/>.</exception>
103
161
// If the type provided is not a Nullable Type, return null.
104
162
// Otherwise, return the underlying type of the Nullable type
/// As the returned readonly reference refers to data that is stored in the input <paramref name="nullable"/> value, this method should only ever be
129
188
/// called when the input reference points to a value with an actual location and not an "rvalue" (an expression that may appear on the right side but not left side of an assignment). That is, if this API is called and the input reference
189
+
/// called when the input reference points to a value with an actual location and not an "rvalue" (an expression that may appear on the right side but not left side of an assignment). That is, if this API is called and the input reference
130
190
/// points to a value that is produced by the compiler as a defensive copy or a temporary copy, the behavior might not match the desired one.
0 commit comments