C# foreach on IEnumerable vs. List - element modification persistent only for array - Why?

前端 未结 2 1762
独厮守ぢ
独厮守ぢ 2021-01-13 19:55

In C#, I have noticed that if I am running a foreach loop on a LINQ generated IEnumerable collection and try to modify the contents of each T element,

相关标签:
2条回答
  • 2021-01-13 20:28

    You are right, it is deferred execution. A new MyClass instance is created each time you iterate the IEnumerable. By calling ToList or ToArray you then create a List or Array and populate it with the new MyClass instances created from the iteration of the IEnumerable.

    0 讨论(0)
  • 2021-01-13 20:31

    Deferred execution is the indeed the key point.

    Executing myClassEnumerable.First().Str will reexecute your query ints.Select(i => new MyClass(i)); and so it will give you a new IEnumerable with a new list of integers.

    You can see this in action using your debugger. Put a breakpoint at the new MyClass(i) part of the IEnumerable select and you will see that this part get's hit again when you execute it for Console.WriteLine

    0 讨论(0)
提交回复
热议问题