Click or drag to resize

XEnumerableToArrayTSource Method

Creates an array from the elements in the source sequence. Unlike ToArrayTSource(IEnumerableTSource), this method takes the number of elements as a parameter, so that it can allocate an array of the right size from the start, hence suppressing the need for subsequent allocations and improving performance.

Namespace:  Linq.Extras
Assembly:  Linq.Extras (in Linq.Extras.dll) Version: 5.0.0+96a4f4bfed64095342c5df107c1fe1fb95603ee5
Syntax
public static TSource[] ToArray<TSource>(
	this IEnumerable<TSource> source,
	int count
)

Parameters

source
Type: System.Collections.GenericIEnumerableTSource
The sequence containing the elements to put in the array.
count
Type: SystemInt32
The number of elements in source.

Type Parameters

TSource
The type of the elements of source.

Return Value

Type: TSource
An array containing the same elements as the source sequence.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Exceptions
ExceptionCondition
IndexOutOfRangeExceptionThe source sequence contains more than count elements.
Remarks
ToArrayTSource(IEnumerableTSource) doesn't know the number of elements in the source sequence (unless it implements ICollectionT), so it starts by allocating a small array, copies elements into it until it's full, then allocates a new array with twice the initial size, copies the data from the previous array, and continues until all elements have been copied; then it needs to trim the array, by allocating yet another array with the correct size and copying the data into it. This is very inefficient because of the many allocations and copies. This method allows you to supply the number of elements, if you know it, so that it can make a single array allocation of exactly the right size.
See Also