I have a construction similar to this (but a lot more complicated):
var list = new List<string>();
// .. populate list ..
foreach(var item in list)
{
DoFunction(list);
}
public void DoFunction(List<string> list)
{
if(someCondition == true)
{
// .. modify list in here ..
}
}
Now, I understand that its not possible to edit the collection you're foreaching through, but how do you jump out of the loop gracefully if you do have to edit the list (without a try catch
statement)? Is there some way to tell if the list has been edited? Can you edit the list and quickly break;
before it notices?
Yes, you could break, if that's what you really want. An exception won't be thrown until the
for
loop tries to grab the next item from the list.But I've found it's easiest just to create and iterate across a copy of the list so you don't have to worry about it.
The added performance overhead of an extra, untouched list is generally negligible compared to the maintainability costs of more complex code.
It's hard to offer useful advice without knowing what kinds of edits are being made. The pattern that I've found is has the most general-purpose value, though, to just construct a new list.
For example, if you need to look at each item and decide between removing it, leaving it as-is, or inserting items after it, you could use a pattern like this:
Rather than use a
foreach
construct, afor
loop would allow you to alter the list.