Click or drag to resize

XEnumerableToListTSource Method

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

Parameters

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

Type Parameters

TSource
The type of the elements of source.

Return Value

Type: ListTSource
A ListT 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).
Remarks
ToListTSource(IEnumerableTSource) doesn't know the number of elements in the source sequence (unless it implements ICollectionT), so it starts with an empty collection and adds the items to it; every time the list's capacity is exceeded, it needs to resize itself, which involves allocating a new array and copying the existing data into it (all of this is actually done in the ListT class). 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 allocate a list with sufficient capacity and avoid subsequent allocations.
See Also