Use _emptyArray in List<T>.ToArray
authorstephentoub <stoub@microsoft.com>
Wed, 20 Jan 2016 23:03:48 +0000 (18:03 -0500)
committerstephentoub <stoub@microsoft.com>
Thu, 21 Jan 2016 00:01:12 +0000 (19:01 -0500)
src/mscorlib/src/System/Collections/Generic/List.cs

index 5a89c24..869a15f 100644 (file)
@@ -1001,12 +1001,19 @@ namespace System.Collections.Generic {
             }
         }
 
-        // ToArray returns a new Object array containing the contents of the List.
+        // ToArray returns an array containing the contents of the List.
         // This requires copying the List, which is an O(n) operation.
         public T[] ToArray() {
             Contract.Ensures(Contract.Result<T[]>() != null);
             Contract.Ensures(Contract.Result<T[]>().Length == Count);
 
+#if FEATURE_CORECLR
+            if (_size == 0)
+            {
+                return _emptyArray;
+            }
+#endif
+
             T[] array = new T[_size];
             Array.Copy(_items, 0, array, 0, _size);
             return array;