I see the following code:
using(var iterator = source.GetEnumerator()) {...}
Where source
is a IEnumerable<T>
.
What is the advantage of doing the above versus converting source
into a List<T>
and then iterating over it?
Converting it to a list will iterate the enumerable once and copy all the references (or even values for value types) into a new List<>
. Then, you would iterate over the list. That means you would iterate twice.
Using the IEnumerable<>
as a source for enumeration iterates over the sequence only once.
Why someone decided to do the iteration manually using the enumerator instead of leaving the details to a foreach
is unclear from the small scope you posted.