I have a collection lets say list of integers and looping through it using foreach, but if I have to insert data into the list during the loop, how do I do that ? I'm getting
Unhandled Exception: System.InvalidOperationException: Collection was modified enumeration operation may not execute.
This is the sample code below. I thought AsReadonly() would return a collection that was prior to modified state. That is not the case here.
ints = new List<int>(30);
ints.AddRange(Enumerable.Range(1, 10));
int y = 11;
foreach(int x in ints.AsReadOnly())
{
ints.Add(y++);
Console.WriteLine(x);
}