I was wondering if it is possible to cast an IEnumerable
to a List
. Is there any way to do it other than copying out each item into a list?

- 23,966
- 9
- 79
- 68

- 38,647
- 50
- 150
- 207
6 Answers
As already suggested, use yourEnumerable.ToList()
. It enumerates through your IEnumerable
, storing the contents in a new List
. You aren't necessarily copying an existing list, as your IEnumerable
may be generating the elements lazily.
This is exactly what the other answers are suggesting, but clearer. Here's the disassembly so you can be sure:
public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
return new List<TSource>(source);
}

- 23,966
- 9
- 79
- 68

- 2,440
- 2
- 22
- 28
-
1will it throw an error if IEnumerable is not null but empty ? – Sumit Joshi Feb 15 '18 at 06:03
-
1@SumitJoshi no it won't throw an error. An IEnumerable can be empty. – Jul 04 '22 at 00:58
using System.Linq;
Use the .ToList() method. Found in the System.Linq namespace.
var yourList = yourEnumerable.ToList();
https://learn.microsoft.com/en-us/dotnet/api/system.linq?view=netcore-2.2

- 16,949
- 65
- 235
- 452

- 24,673
- 10
- 77
- 110
-
5If not available using System.Linq is missing :). This should totally be the accepted answer... – CodingYourLife Feb 02 '17 at 16:55
-
1Also if it's not available, you may have forgotten to declare your IEnumerable with its
. – Keavon Feb 11 '19 at 22:52 -
Very simple and to the point, thanks! Maybe use a null-conditional operator in `yourEnumerable?.ToList();` to take into account the possibility that `yourEnumerable` might be `null`? – Jean-David Lanz Nov 17 '20 at 09:14
As others suggested, simply use the ToList()
method on an enumerable object:
var myList = myEnumerable.ToList()
But, if your object implementing the IEnumerable
interface doesn't have the ToList()
method and you're getting an error like the following:
'IEnumerable' does not contain a definition for 'ToList'
...you're probably missing the System.Linq
namespace, because the ToList()
method is an extension method provided by that namespace, it's not a member of the IEnumerable
interface itself.
So just add the namespace to your source file:
using System.Linq

- 23,966
- 9
- 79
- 68
Create a new List and pass the old IEnumerable to its initializer:
IEnumerable<int> enumerable = GetIEnumerable<T>();
List<int> list = new List<int>(enumerable);

- 101,809
- 122
- 424
- 632
Another gotcha (async call)
An async call may be your problem. If you added the using System.Linq statement and you are still getting the error "does not contain a definition for 'ToList' and no accessible extension method...", look carefully for the Task keyword in your error message.
Original Call (works)
IEnumerable<MyDocument> docList = await _documentRepository.GetListAsync();
Attempt to use ToList (still not working)
So...if you are doing this and it does NOT work
List<MyDocument> docList = await _documentRepository.GetListAsync().ToList();
Use parenthesis
You are actually calling ToList on the Task<IEnumerable>! Add parenthesis around your await call like this
List<MyDocument> docList = (await _documentRepository.GetListAsync()).ToList();

- 1,935
- 2
- 22
- 38
no, you should copy, if you are sure that the reference is reference to list, you can convert like this
List<int> intsList = enumIntList as List<int>;

- 49,896
- 32
- 148
- 184
-
5If you're *sure* that it's a reference to a list, you should use a direct cast so that it will throw an exception if you're wrong. Use "as" if you think it *might* be a List
but you're not sure, and neither are error conditions. Then test whether the result is null. – Jon Skeet Jun 07 '09 at 07:31 -
Maybe add an 'if (intsList == null) intsList = new List
(enumIntList);' if it *might* be a 'List – jerryjvl Jun 07 '09 at 08:22', already, but there are some cases where it is not.