Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit c2481cd

Browse files
kasper3ahsonkhan
authored andcommitted
Return empty array if length is zero (#16529)
* Return empty array if length is zero * Return true * Add support for portable system * Use portable span helper instead of Array.Empty * Move to end * Remove else * Return empty array if length is 0 in MemoryMarshal
1 parent 9f6d344 commit c2481cd

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/mscorlib/shared/System/Memory.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ public bool TryGetArray(out ArraySegment<T> arraySegment)
329329
return true;
330330
}
331331

332+
if (_length == 0)
333+
{
334+
#if FEATURE_PORTABLE_SPAN
335+
arraySegment = new ArraySegment<T>(SpanHelpers.PerTypeValues<T>.EmptyArray);
336+
#else
337+
arraySegment = ArraySegment<T>.Empty;
338+
#endif // FEATURE_PORTABLE_SPAN
339+
return true;
340+
}
341+
332342
arraySegment = default(ArraySegment<T>);
333343
return false;
334344
}

src/mscorlib/shared/System/Runtime/InteropServices/MemoryMarshal.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public static bool TryGetArray<T>(ReadOnlyMemory<T> readOnlyMemory, out ArraySeg
3535
return true;
3636
}
3737

38+
if (length == 0)
39+
{
40+
#if FEATURE_PORTABLE_SPAN
41+
arraySegment = new ArraySegment<T>(SpanHelpers.PerTypeValues<T>.EmptyArray);
42+
#else
43+
arraySegment = ArraySegment<T>.Empty;
44+
#endif // FEATURE_PORTABLE_SPAN
45+
return true;
46+
}
47+
3848
arraySegment = default;
3949
return false;
4050
}

0 commit comments

Comments
 (0)