Skip to content

Commit 3cf3175

Browse files
authored
Add IntelliSense comments to Nullable classes (#254)
***NO_CI***
1 parent 412684a commit 3cf3175

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

nanoFramework.CoreLibrary/System/Nullable.cs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
#pragma warning disable CA1066 // Implement IEquatable when overriding Object.Equals
5+
#nullable enable
56

67
namespace System
78
{
@@ -14,26 +15,46 @@ namespace System
1415
/// <summary>
1516
/// Represents a value type that can be assigned <see langword="null"/>.
1617
/// </summary>
17-
/// <typeparam name="T"></typeparam>
18+
/// <typeparam name="T">The underlying value type of the <see cref="Nullable{T}"/> generic type.</typeparam>
1819
[Serializable]
1920
public partial struct Nullable<T> where T : struct
2021
{
2122
// Do not rename (binary serialization)
2223
private readonly bool hasValue;
2324
// Do not rename (binary serialization) or make readonly (can be mutated in ToString, etc.)
24-
internal T value;
25+
internal T value;
2526

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>
2634
public Nullable(T value)
2735
{
2836
this.value = value;
2937
hasValue = true;
3038
}
3139

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>
3246
public readonly bool HasValue
3347
{
3448
get => hasValue;
3549
}
3650

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>
3758
public readonly T Value
3859
{
3960
get
@@ -46,11 +67,27 @@ public readonly T Value
4667
}
4768
}
4869

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>
4977
public readonly T GetValueOrDefault() => value;
5078

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>
5187
public readonly T GetValueOrDefault(T defaultValue) =>
5288
hasValue ? value : defaultValue;
5389

90+
/// <inheritdoc />
5491
public override bool Equals(object? other)
5592
{
5693
if (!hasValue)
@@ -66,16 +103,31 @@ public override bool Equals(object? other)
66103
return value.Equals(other);
67104
}
68105

106+
/// <inheritdoc />
69107
public override int GetHashCode() => hasValue ? value.GetHashCode() : 0;
70108

109+
/// <inheritdoc />
71110
public override string? ToString() => hasValue ? value.ToString() : "";
72111

112+
/// <summary>
113+
/// Creates a new <see cref="Nullable{T}"/> object initialized to a specified value.
114+
/// </summary>
115+
/// <param name="value">A value type.</param>
116+
/// <returns>A <see cref="Nullable{T}"/> object whose <see cref="Value"/> property is initialized with the value parameter.</returns>
73117
public static implicit operator T?(T value) =>
74118
new T?(value);
75119

120+
/// <summary>
121+
/// Defines an explicit conversion of a <see cref="Nullable{T}"/> instance to its underlying value.
122+
/// </summary>
123+
/// <param name="value">A nullable value.</param>
124+
/// <returns>The value of the <see cref="Value"/> property of the <paramref name="value"/> parameter.</returns>
76125
public static explicit operator T(T? value) => value!.Value;
77126
}
78127

128+
/// <summary>
129+
/// Supports a value type that can be assigned <see langword="null"/>. This class cannot be inherited.
130+
/// </summary>
79131
public static class Nullable
80132
{
81133
//public static int Compare<T>(T? n1, T? n2) where T : struct
@@ -100,6 +152,12 @@ public static class Nullable
100152
// return true;
101153
//}
102154

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>
103161
// If the type provided is not a Nullable Type, return null.
104162
// Otherwise, return the underlying type of the Nullable type
105163
public static Type? GetUnderlyingType(Type nullableType)
@@ -110,6 +168,7 @@ public static class Nullable
110168
{
111169
// Instantiated generic type only
112170
Type genericType = nullableType.GetGenericTypeDefinition();
171+
113172
if (ReferenceEquals(genericType, typeof(Nullable<>)))
114173
{
115174
return nullableType.GetGenericArguments()[0];
@@ -127,6 +186,7 @@ public static class Nullable
127186
/// <remarks>
128187
/// As the returned readonly reference refers to data that is stored in the input <paramref name="nullable"/> value, this method should only ever be
129188
/// 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
130190
/// 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.
131191
/// </remarks>
132192
public static ref readonly T GetValueRefOrDefaultRef<T>(ref readonly T? nullable)

0 commit comments

Comments
 (0)