.net – Convert IEnumerable To List

.netcollectionsienumerablelinqlist

If i want to convert Enumerable (IEnumerable<T>) to list.

What is more efficient:

  1. myEnumerable.ToList()

  2. or new List<T>(myEnumerable)

Thanks!

Best Solution

They take more or less the same time to execute but new List<T>(myEnumerable) is a bit faster.

myEnumerable.ToList() will do a new List<T>(myEnumerable) under the hood, but it will first check if the collection is null and throw an exception if it is. If you know that myEnumerable is not null then you could use the ToList().

But, in my opinion, write the code that is easiest to read. This is a micro optimization that you should not spend too much time on.