Skip to content

Commit

Permalink
Merge pull request dotnet#2758 from stephentoub/list_toarray
Browse files Browse the repository at this point in the history
Use _emptyArray in List<T>.ToArray
  • Loading branch information
stephentoub committed Jan 21, 2016
2 parents d67baec + 5bcecb9 commit 9a43c44
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/mscorlib/src/System/Collections/Generic/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,12 +1001,19 @@ public void Sort(Comparison<T> comparison) {
}
}

// 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;
Expand Down

0 comments on commit 9a43c44

Please sign in to comment.