Optimise ToArray for list source

This commit is contained in:
Jon Hanna
2017-01-04 15:01:48 +00:00
parent 5bd4521b53
commit e08d1f7524
+17 -9
View File
@@ -793,17 +793,25 @@ namespace Newtonsoft.Json.Utilities.LinqBridge
return new List<TSource>(source); return new List<TSource>(source);
} }
/// <summary> /// <summary>
/// Creates an array from an <see cref="IEnumerable{T}"/>. /// Creates an array from an <see cref="IEnumerable{T}"/>.
/// </summary> /// </summary>
public static TSource[] ToArray<TSource>( public static TSource[] ToArray<TSource>(
this IEnumerable<TSource> source) this IEnumerable<TSource> source)
{ {
return source.ToList().ToArray(); IList<TSource> ilist = source as IList<TSource>;
} if (ilist != null)
{
TSource[] array = new TSource[ilist.Count];
ilist.CopyTo(array, 0);
return array;
}
/// <summary> return source.ToList().ToArray();
}
/// <summary>
/// Returns distinct elements from a sequence by using the default /// Returns distinct elements from a sequence by using the default
/// equality comparer to compare values. /// equality comparer to compare values.
/// </summary> /// </summary>