
See if you can spot the problem with the following C# code:
var list = new List
for (int i = 0; i < list.Count; i++)
{
list.Add(list[i].ToUpper());
}
As the title points out, this will cause an infinite loop, and will eat up all of your memory. What happens is that we iterate through the list once (i=0; list.Count = 3) and then add an element to the list. So the next loop we have (i=1; list.Count = 4). This is because anything in the parenthesis of the for operator will be evaluated every loop.
There are three options:
And the code was a dummy example code, nothing in use in any real system.