Was just wondering if it is unsafe / bad form / just kind of looks dirty to remove a single element from a Collection while in a foreach loop without saving it to do outside the loop. I know if you had to keep iterating, it could break the loop (and in some cases, just throw an error), but in this case you do not need to iterate again because you are using a break statement when you find what you are looking for.
Example:
List<string> strings = new List<string>() { "A", "B", "C" };
foreach(string s in strings)
{
if (s == "B")
{
strings.Remove("B");
break;
}
}
Is this bad? I would normally be writing this as either a backwards loop or caching the stringToRemove in a variable (with the obvious name stringToRemove), but just wondered how something like this felt to other folks.