How does the extension method ToList()
work? Say I have an IEnumerable
of 10000 items. Will ToList()
create a new List
and iterate over the IEnumerable
of 10000 items and then return me a List
or does .NET do it in some other way?
This MSDN link talks about immediate execution of a DB query. My question is only about converting IEnumerable
to a List
.
The tolist() function is used to convert a given array to an ordinary list with the same items, elements, or values.
ToList() simply creates a new List that contains a private array that is added to during each iteration. The very first iteration will create a new array with defaultCapacity = 4 , just like in ToArray() . Inside the Capacity setter, a new array which is doubled in size, is created when the initial array is full.
ToList() makes a shallow copy. The references are copied, but the new references still point to the same instances as the original references point to.
LINQ ToList() Method In LINQ, the ToList operator takes the element from the given source, and it returns a new List. So, in this case, input would be converted to type List.
If you use a narrower interface type such as IEnumerable instead of IList, you protect your code against breaking changes. If you use IEnumerable, the caller of your method can provide any object which implements the IEnumerable interface. These are nearly all collection types of the base class library and in addition many custom defined types.
C#’s IEnumerable and IEnumerator interfaces are used by collections like Arrays and Lists to standardize methods of looping over them and performing actions, like filtering and selecting with LINQ statements. We’ll discuss how they work, and how to use them. What Is an Enumerable?
For example, by returning a List, you've taken away the ability to stream results which can have its own performance benefits. Your implementation may change, but the caller can always turn an IEnumerable into a List if they need to. Now that you've decided what the return type should be, you have to decide what the implementation should use.
The ToList<TSource> (IEnumerable<TSource>) method forces immediate query evaluation and returns a List<T> that contains the query results. You can append this method to your query in order to obtain a cached copy of the query results.
It doesn't necessarily iterate, although that's the "worst case" scenario. Basically it calls new List<T>(source)
, but that has some tricks up its sleeve: if the source implements ICollection<T>
, the constructor can call ICollection<T>.CopyTo()
to copy the complete data into an array. This may well be implemented more efficiently than single-stepping iteration. Likewise in the ICollection<T>
case, the new list knows the final size to start with, so it won't need to keep expanding its internal buffers.
For a few more details, see my Edulinq ToList()
blog post.
The .ToList
extension method calls the List<T>
constructor passing it the IEnumerable<T>
. This constructor will iterate over the IEnumerable<T>
and copy the elemtns of the IEnumerable<T>
in the same order they are returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With