{"record":{"id":"15b3a96792ae1315","repo":"dotnet/wpf","slug":"sr-enumerator-reachedend-transformcollection","errorCode":null,"errorMessage":"SR.Enumerator_ReachedEnd","messagePattern":"SR\\.Enumerator_ReachedEnd","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TransformCollection.cs","lineNumber":889,"sourceCode":"            /// off the end of the list. However, the IEnumerable.Current\n            /// contract requires that we throw exceptions\n            /// </summary>\n            public Transform Current\n            {\n                get\n                {\n                    if (_index > -1)\n                    {\n                        return _current;\n                    }\n                    else if (_index == -1)\n                    {\n                        throw new InvalidOperationException(SR.Enumerator_NotStarted);\n                    }\n                    else\n                    {\n                        Debug.Assert(_index == -2, \"expected -2, got \" + _index + \"\\n\");\n                        throw new InvalidOperationException(SR.Enumerator_ReachedEnd);\n                    }\n                }\n            }\n\n            #endregion\n\n            #region Data\n            private Transform _current;\n            private TransformCollection _list;\n            private uint _version;\n            private int _index;\n            #endregion\n        }\n        #endregion\n\n        #region Constructors\n\n        //------------------------------------------------------","sourceCodeStart":871,"sourceCodeEnd":907,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TransformCollection.cs#L871-L907","documentation":"TransformCollection.Enumerator.Current throws InvalidOperationException with Enumerator_ReachedEnd when accessed after MoveNext() returned false (past the end, _index == -2). There is no current element once enumeration has completed.","triggerScenarios":"Reading Current after MoveNext() returned false, or accessing Current again after a completed foreach; any code that keeps the enumerator alive past the last element and dereferences Current.","commonSituations":"Loops that read Current after the loop 'just in case'; storing the enumerator and reading Current later; do/while loops that check Current before MoveNext's return value.","solutions":["Only read Current when MoveNext() returned true; capture the value inside the loop body.","Cache the last valid Current item in a local variable before the loop ends if you need it afterwards.","Use foreach to make out-of-range Current access impossible.","Catch InvalidOperationException around Current access if legacy code cannot be restructured."],"exampleFix":"// before\nTransform last = null;\nvar e = transformCollection.GetEnumerator();\nwhile (e.MoveNext()) { }\nlast = e.Current; // throws: past end\n// after\nTransform last = null;\nforeach (Transform t in transformCollection)\n{\n    last = t;\n}","handlingStrategy":"validation","validationCode":"// Only read Current when MoveNext() returned true:\nwhile (enumerator.MoveNext())\n{\n    var current = enumerator.Current; // safe inside loop\n}","typeGuard":"static Transform LastOrDefault(TransformCollection c)\n{\n    if (c == null || c.Count == 0) return null;\n    return c[c.Count - 1]; // index access instead of enumerator Current\n}","tryCatchPattern":"try\n{\n    var cur = enumerator.Current;\n}\ncatch (InvalidOperationException)\n{\n    // enumeration already past end; use the last value captured inside the loop\n}","preventionTips":["Capture Current into a local variable inside the loop; never read it after the loop.","Use the collection indexer (collection[Count-1]) instead of an exhausted enumerator to get the last item.","Prefer foreach or LINQ over manual enumerator lifecycle."],"tags":["wpf","enumerator","invalidoperationexception","ienumerable"],"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"}