XEnumerableToListTSource Method |
Creates a
ListT from the elements in the source sequence. Unlike
ToListTSource(IEnumerableTSource),
this method takes the number of elements as a parameter, so that it can allocate a list with sufficient capacity, 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
Syntaxpublic static List<TSource> ToList<TSource>(
this IEnumerable<TSource> source,
int count
)
<ExtensionAttribute>
Public Shared Function ToList(Of TSource) (
source As IEnumerable(Of TSource),
count As Integer
) As List(Of TSource)
[<ExtensionAttribute>]
static member ToList :
source : IEnumerable<'TSource> *
count : int -> List<'TSource>
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:
ListTSourceA
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).
RemarksToListTSource(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