Code inspection: Possible '{0}.{1}: Collection was modified'
This inspection reports a foreach loop that modifies the same collection it is enumerating. That can throw InvalidOperationException at runtime with a message like "Collection was modified".
Example
foreach (var item in items)
{
items.Add(item);
}
foreach (var item in items.ToList())
{
items.Add(item);
}
Quick-fix
The quick-fix copies the elements before enumeration so the loop iterates over a stable snapshot.
01 April 2026