Skip to content

Commit 418af71

Browse files
committed
Fix build error on .NET 6
1 parent 255c4e1 commit 418af71

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

CommunityToolkit.HighPerformance/Extensions/NullableExtensions.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ public static ref T DangerousGetValueOrDefaultReference<T>(this ref T? value)
5050
/// <returns>A reference to the value of the input <see cref="Nullable{T}"/> instance, or a <see langword="null"/> <typeparamref name="T"/> reference.</returns>
5151
/// <remarks>The returned reference can be tested for <see langword="null"/> using <see cref="Unsafe.IsNullRef{T}(ref T)"/>.</remarks>
5252
[MethodImpl(MethodImplOptions.AggressiveInlining)]
53-
public static unsafe ref T DangerousGetValueOrNullReference<T>(ref this T? value)
53+
public static ref T DangerousGetValueOrNullReference<T>(ref this T? value)
5454
where T : struct
5555
{
56+
#if NET7_0_OR_GREATER
5657
ref T resultRef = ref Unsafe.NullRef<T>();
5758

5859
// This pattern ensures that the resulting code ends up having a single return, and a single
@@ -69,14 +70,18 @@ public static unsafe ref T DangerousGetValueOrNullReference<T>(ref this T? value
6970
// This is better than what the code would've been with two separate returns in the method.
7071
if (value.HasValue)
7172
{
72-
#if NET7_0_OR_GREATER
7373
resultRef = ref Unsafe.AsRef(in Nullable.GetValueRefOrDefaultRef(in value));
74-
#else
75-
resultRef = ref Unsafe.As<T?, RawNullableData<T>>(ref value).Value;
76-
#endif
7774
}
7875

7976
return ref resultRef;
77+
#else
78+
if (value.HasValue)
79+
{
80+
return ref Unsafe.As<T?, RawNullableData<T>>(ref value).Value;
81+
}
82+
83+
return ref Unsafe.NullRef<T>();
84+
#endif
8085
}
8186

8287
#if !NET7_0_OR_GREATER

0 commit comments

Comments
 (0)