C# Why IEnumerable and IEnumerable.ToList() print their result is difference?
When you call numbers.Count()
, you're enumerating the IEnumerable
again. Because the IEnumerable is enumerated twice, the Return lines are each printed twice.
The list conversion enumerates the IEnumerable
to build the list, but after that, only the resulting List<int>
is used by Print
. When .Count()
is called on the List, which just returns the built-in Count
property of List<int>
and doesn't enumerate anything.