How to easily convert from IEnumerable <T> to list <T>?
4 answers
You can use ToListthe extension method on IEnumerable<T>or , which accepts . List<T>IEnumerable<T>
IEnumerable<int> numbers = ...;
List<int> numberList = numbers.ToList();
List<int> numberList2 = new List<int>(numbers);
Regardless of what others have said, you will have to list the original Enumerable<int>(whether you do it manually or not), although it is possible that one or both of these methods can work more efficiently than a naive enumeration if the original IEnumerableis equal to List<T>or ICollection<T>.
, IEnumerable, IEnumerable<T>, , Cast<T> IEnumerable
+3