{"record":{"id":"474dd917583d8bb9","repo":"dotnet/wpf","slug":"sr-enumeratorreachedend","errorCode":null,"errorMessage":"SR.EnumeratorReachedEnd","messagePattern":"SR\\.EnumeratorReachedEnd","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs","lineNumber":347,"sourceCode":"                    _currentElement = _collection;\n                    _index = _collection.Size;\n                    return (false);\n                }\n            }\n\n            public TItem Current\n            {\n                get\n                {\n                    if (_currentElement == _collection)\n                    {\n                        if (_index == -1)\n                        {\n                            throw new InvalidOperationException(SR.EnumeratorNotStarted);\n                        }\n                        else\n                        {\n                            throw new InvalidOperationException(SR.EnumeratorReachedEnd);\n                        }\n                    }\n                    return (TItem)_currentElement;\n                }\n            }\n\n            /// <summary>\n            ///     <see cref=\"IEnumerator.Current\"/>\n            /// </summary>\n            object IEnumerator.Current\n            {\n                get\n                {\n                    return this.Current;\n                }\n            }\n\n","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs#L329-L365","documentation":"The enumerator's Current property throws InvalidOperationException with SR.EnumeratorReachedEnd when Current is read after MoveNext returned false (index past the last element). Once iteration has finished there is no valid current element, so the sentinel check raises instead of returning a stale or null value.","triggerScenarios":"Continuing to read Current after MoveNext() returned false, e.g. a loop that reads Current unconditionally one extra time, or caching the enumerator and reading Current after a completed pass.","commonSituations":"while(true) loops with a broken termination check, reusing a finished enumerator for a 'last element' access, or LINQ operators applied to an already-exhausted enumerator reference.","solutions":["Only read Current when the preceding MoveNext() returned true","Call Reset() (or get a fresh enumerator) before iterating again","Capture the last item inside the loop instead of reading Current afterward"],"exampleFix":"// before\nwhile (e.MoveNext()) { }\nvar last = e.Current;\n// after\nobject last = null;\nwhile (e.MoveNext()) { last = e.Current; }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"object last = null; while (enumerator.MoveNext()) { last = enumerator.Current; } /* do not read enumerator.Current after the loop */","preventionTips":["Read Current only when MoveNext() returned true","Capture values inside the loop instead of after exhaustion","Call Reset() or obtain a fresh enumerator to iterate again"],"tags":["enumerator-state","wpf","collections"],"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-21T21:30:21.729Z"}