Could someone take the time to explain me the language underpinnings here :
int foo = myObject.SomeList.Count;
for (int i = 0 ; i < foo ; i++)
{
myObject.SomeList.Add(bar);
}
goes into an infinite loop because foo
references a value that keeps being incremented. Modifying the first line to:
int foo = (int)myObject.SomeList.Count;
makes it go away, somehow changing foo
from reference to value once and for all. This is probably textbook but, why is this happening ?
Many thanks
EDIT : Ok, as Patrick mentions, the infinite loop only happens when there is no prior storage to foo, the cast being pointless, which makes sense. That's indeed what I thought initially while debugging this. Hence my surprise when the cast fixed it. What actually happened is that I was misled by Visual Studio into thinking I had fixed it when there was actually a problem of synchronization between edited code and executed code, which led to wrong conclusions.