I have following code snippet in c#.
var list = new List<string> { "a", "b", "c" };
for (int i = 0; i < list.Count; i++)
{
list.Add(list[i].ToUpper());
}
There is no compile time error in above code,but I am gettingSystem.OutOfMemoryException exception at runtime?
List.Countreevaluating each time you add an element.So, you have 3 elements. After the first iteration of your for loop you will have
Count== 4. Soinever reachCountvalue (until integer overflow case, but out of memory happens earlier in your case).You can write smth. like: