{"record":{"id":"92da258f94e4f951","repo":"dotnet/wpf","slug":"sr-enumerator-reachedend-textdecorationcollection","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/Generated/TextDecorationCollection.cs","lineNumber":845,"sourceCode":"            /// off the end of the list. However, the IEnumerable.Current\n            /// contract requires that we throw exceptions\n            /// </summary>\n            public TextDecoration 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 TextDecoration _current;\n            private TextDecorationCollection _list;\n            private uint _version;\n            private int _index;\n            #endregion\n        }\n        #endregion\n\n        #region Constructors\n\n        //------------------------------------------------------","sourceCodeStart":827,"sourceCodeEnd":863,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs#L827-L863","documentation":"The enumerator's Current property throws InvalidOperationException (SR.Enumerator_ReachedEnd) when accessed after enumeration has completed (_index == -2, i.e. MoveNext returned false or Reset-terminated state). Current cannot be read past the end of the collection.","triggerScenarios":"Reading Current after MoveNext() returned false, or re-reading Current in code after a completed foreach (accessing a stored enumerator).","commonSituations":"Loop that reads Current after the terminating MoveNext; capturing enumerator.Current in a closure executed later; while-loop patterns that read before checking MoveNext's return on the final iteration.","solutions":["Read Current only when MoveNext() returns true, and store the value locally inside the loop","Track your own 'last element' variable instead of re-reading Current after the loop","Use foreach which guarantees Current is never read past the end"],"exampleFix":"// before\nwhile (e.MoveNext()) { }\nvar last = e.Current;\n// after\nobject last = null;\nwhile (e.MoveNext()) { last = e.Current; }","handlingStrategy":"validation","validationCode":"// capture the value inside the loop instead of after it\nobject last = null;\nwhile (enumerator.MoveNext()) { last = enumerator.Current; }","typeGuard":null,"tryCatchPattern":"InvalidOperationException ex -> stop reading Current once MoveNext() returned false; keep a local copy of the last item","preventionTips":["Never read Current after MoveNext() returns false","Store the last item in a local variable during the loop","Use foreach, which never reads Current past the end"],"tags":["wpf","invalidoperationexception","enumerator","current"],"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"}