{"record":{"id":"1b32584fb1ac9094","repo":"AvaloniaUI/Avalonia","slug":"collection-was-modified-during-enumeration-1b3258","errorCode":null,"errorMessage":"Collection was modified during enumeration.","messagePattern":"Collection was modified during enumeration\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Collections/Pooled/ThrowHelper.cs","lineNumber":306,"sourceCode":"            throw new InvalidOperationException(\"Enumeration has not started.\");\n        }\n\n        [DoesNotReturn]\n        internal static void ThrowInvalidOperationException_InvalidOperation_EnumEnded()\n        {\n            throw new InvalidOperationException(\"Enumeration has ended.\");\n        }\n\n        [DoesNotReturn]\n        internal static void ThrowInvalidOperationException_EnumCurrent(int index)\n        {\n            throw GetInvalidOperationException_EnumCurrent(index);\n        }\n\n        [DoesNotReturn]\n        internal static void ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion()\n        {\n            throw new InvalidOperationException(\"Collection was modified during enumeration.\");\n        }\n\n        [DoesNotReturn]\n        internal static void ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen()\n        {\n            throw new InvalidOperationException(\"Invalid enumerator state: enumeration cannot proceed.\");\n        }\n\n        [DoesNotReturn]\n        internal static void ThrowInvalidOperationException_InvalidOperation_NoValue()\n        {\n            throw new InvalidOperationException(\"No value provided.\");\n        }\n\n        [DoesNotReturn]\n        internal static void ThrowInvalidOperationException_ConcurrentOperationsNotSupported()\n        {\n            throw new InvalidOperationException(\"Concurrent operations are not supported.\");","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Collections/Pooled/ThrowHelper.cs#L288-L324","documentation":"Centralized ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion throws InvalidOperationException \"Collection was modified during enumeration.\" It is actively used by PooledList<T>: in ForEach (after the loop if the version changed), in Enumerator.MoveNextRare, and in Enumerator.Reset — all triggered when PooledList's _version changed since the enumerator/action captured it. It is the canonical fail-fast for concurrent/recursive mutation during enumeration.","triggerScenarios":"Adding/removing/clearing/sorting a PooledList while a foreach or manual MoveNext loop over it is live; calling Reset() after mutating; ForEach whose callback mutates the list.","commonSituations":"Mutating the list inside a foreach body; event reentrancy that adds/removes items; sorting or clearing during iteration; concurrent access from another thread.","solutions":["Iterate over a snapshot: `foreach (var x in list.ToArray())` or `list.Span` copied out.","Defer mutations: collect items to add/remove during the loop and apply them after.","For ForEach, do not mutate the list inside the action; use a plain for loop with index bounds instead."],"exampleFix":"// before\nforeach (var item in list)\n    list.Remove(item); // mutates during enumeration\n\n// after\nforeach (var item in list.ToArray())\n    list.Remove(item);","handlingStrategy":"validation","validationCode":"foreach (var item in list.ToArray()) // snapshot before mutating\n    list.Remove(item);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never add/remove/clear/sort a PooledList inside a foreach over it.","Collect changes during iteration, apply them after.","For ForEach, do not mutate the list inside the action."],"tags":["csharp","pooled","list","enumeration","invalid-operation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}