diff options
author | Jb Evain <jbevain@gmail.com> | 2008-05-09 01:25:28 +0400 |
---|---|---|
committer | Jb Evain <jbevain@gmail.com> | 2008-05-09 01:25:28 +0400 |
commit | a7dfd8b42f4cb462c6b64cc876ab4aef080646b1 (patch) | |
tree | 936c6c07afb2ccdd024f2870849655a1f90393c4 /mcs/class/System.Core/System.Linq/Enumerable.cs | |
parent | 6915192aa97a1c6cd7fd4d75c569c691b5b37f3f (diff) |
optimize ToArray a bit
svn path=/trunk/mcs/; revision=102836
Diffstat (limited to 'mcs/class/System.Core/System.Linq/Enumerable.cs')
-rw-r--r-- | mcs/class/System.Core/System.Linq/Enumerable.cs | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/mcs/class/System.Core/System.Linq/Enumerable.cs b/mcs/class/System.Core/System.Linq/Enumerable.cs index 73f511ab13b..fe7614b1e52 100644 --- a/mcs/class/System.Core/System.Linq/Enumerable.cs +++ b/mcs/class/System.Core/System.Linq/Enumerable.cs @@ -2209,12 +2209,19 @@ namespace System.Linq #endregion #region ToArray + public static TSource [] ToArray<TSource> (this IEnumerable<TSource> source) { Check.Source (source); - List<TSource> list = new List<TSource> (source); - return list.ToArray (); + var collection = source as ICollection<TSource>; + if (collection != null) { + var array = new TSource [collection.Count]; + collection.CopyTo (array, 0); + return array; + } + + return new List<TSource> (source).ToArray (); } #endregion |