更新时间:2022-05-06 15:50:38
如已经建议的那样,使用yourEnumerable.ToList()
.它通过您的IEnumerable
枚举,并将内容存储在新的List
中.您不一定要复制现有列表,因为您的IEnumerable
可能会懒惰地生成元素.
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);
}