0
オブジェクトの動的リストをループするスニペットを実装しようとしています。オブジェクトの動的リストを使ったループ
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var l = new List<int>();
l.Add(1);
l.Add(2);
l.Add(3);
l.Add(4);
foreach(var i in l){
Console.WriteLine(i);
if(i==3){
l.Add(5);
}
}
}
}
これは実行時エラーを下回っています。
1
2
3
Run-time exception (line 15): Collection was modified; enumeration operation may not execute.
Stack Trace:
[System.InvalidOperationException: Collection was modified; enumeration operation may not execute.]
at Program.Main(): line 15
助けていただければ幸いです。ありがとう。
https://dotnetfiddle.net/f6gawa – Reddy
で
foreach
を交換することによって達成することができますOPと同じ意味です – StriplingWarrior私はそれを逃した@ StriplingWarrior。修正されました。 – Reddy