I have this loop:
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
int listCount = list.Count;
for (int i = 0; i < listCount; i++) // Loop through List with for
{
if (i == 2)
{
list.Add(666);
listCount++;
}
System.Diagnostics.Debug.Write(list[i]);
}
I am wondering how do I apply the same logic to foreach loop? (add more elements inside the foreach loop).
Something like this:
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
foreach (int i in list)
{
if (i == 2)
{
list.Add(666);
//do something to refresh the foreach loop
}
System.Diagnostics.Debug.Write(list[i]);
}