Return empty array if length is zero (#16529)
authorkasper3 <33230602+kasper3@users.noreply.github.com>
Sat, 24 Feb 2018 20:38:21 +0000 (22:38 +0200)
committerAhson Khan <ahkha@microsoft.com>
Sat, 24 Feb 2018 20:38:21 +0000 (12:38 -0800)
* 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

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

index c220274..4a1ce4d 100644 (file)
@@ -329,6 +329,16 @@ namespace System
                 return true;
             }
 
+            if (_length == 0)
+            {
+#if FEATURE_PORTABLE_SPAN
+                arraySegment = new ArraySegment<T>(SpanHelpers.PerTypeValues<T>.EmptyArray);
+#else
+                arraySegment = ArraySegment<T>.Empty;
+#endif // FEATURE_PORTABLE_SPAN
+                return true;
+            }
+
             arraySegment = default(ArraySegment<T>);
             return false;
         }
index 7fabf1e..6544081 100644 (file)
@@ -35,6 +35,16 @@ namespace System.Runtime.InteropServices
                 return true;
             }
 
+            if (length == 0)
+            {
+#if FEATURE_PORTABLE_SPAN
+                arraySegment = new ArraySegment<T>(SpanHelpers.PerTypeValues<T>.EmptyArray);
+#else
+                arraySegment = ArraySegment<T>.Empty;
+#endif // FEATURE_PORTABLE_SPAN
+                return true;
+            }
+
             arraySegment = default;
             return false;
         }