{"record":{"id":"4c40dfcb126017a8","repo":"dotnet/wpf","slug":"sr-enumerator-collectionchanged-pathsegmentcollection","errorCode":null,"errorMessage":"SR.Enumerator_CollectionChanged","messagePattern":"SR\\.Enumerator_CollectionChanged","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PathSegmentCollection.cs","lineNumber":797,"sourceCode":"            {\n                _list.ReadPreamble();\n\n                if (_version == _list._version)\n                {\n                    if (_index > -2 && _index < _list._collection.Count - 1)\n                    {\n                        _current = _list._collection[++_index];\n                        return true;\n                    }\n                    else\n                    {\n                        _index = -2; // -2 indicates \"past the end\"\n                        return false;\n                    }\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.Enumerator_CollectionChanged);\n                }\n            }\n\n            /// <summary>\n            /// Sets the enumerator to its initial position, which is before the\n            /// first element in the collection.\n            /// </summary>\n            public void Reset()\n            {\n                _list.ReadPreamble();\n\n                if (_version == _list._version)\n                {\n                    _index = -1;\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.Enumerator_CollectionChanged);","sourceCodeStart":779,"sourceCodeEnd":815,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PathSegmentCollection.cs#L779-L815","documentation":"PathSegmentCollection's Enumerator.MoveNext throws Enumerator_CollectionChanged (InvalidOperationException) when the underlying collection's version changed after the enumerator was created. The enumerator is fail-fast: any Add, Remove, Insert, or Clear between MoveNext calls invalidates it.","triggerScenarios":"Modifying the PathSegmentCollection (Add/Remove/Insert/Clear, or changing a segment that raises collection change) while a foreach or explicit Enumerator is iterating it.","commonSituations":"Mutating path segments inside a foreach loop, a property-changed or rendering callback altering the collection during enumeration, or multi-threaded access where one thread enumerates while another modifies.","solutions":["Do not modify the collection while enumerating; collect pending changes in a separate list and apply after the loop.","Iterate over a snapshot: foreach (var s in collection.ToArray()).","Use a for loop over indices if you must mutate during traversal (and adjust the index accordingly).","Synchronize access across threads (lock or freeze the collection for cross-thread reads)."],"exampleFix":"// before\nforeach (var seg in segments) { if (bad(seg)) segments.Remove(seg); }\n// after\nforeach (var seg in segments.ToArray()) { if (bad(seg)) segments.Remove(seg); }","handlingStrategy":"try-catch","validationCode":"// snapshot before iterating\nvar snapshot = segments.ToArray();","typeGuard":null,"tryCatchPattern":"try { foreach (var s in segments) { Process(s); } }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"collection\")) { /* re-enumerate after mutations settle */ }","preventionTips":["Never mutate a collection inside foreach; buffer changes and apply after","Enumerate a snapshot (ToArray) when mutation is possible","Freeze the collection for read-only cross-thread use"],"tags":["wpf","collection","enumerator"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}