{"record":{"id":"f0bf6c679165f381","repo":"dotnet/wpf","slug":"sr-enumerator-reachedend-generaltransformcollection","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/GeneralTransformCollection.cs","lineNumber":854,"sourceCode":"            /// off the end of the list. However, the IEnumerable.Current\n            /// contract requires that we throw exceptions\n            /// </summary>\n            public GeneralTransform 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 GeneralTransform _current;\n            private GeneralTransformCollection _list;\n            private uint _version;\n            private int _index;\n            #endregion\n        }\n        #endregion\n\n        #region Constructors\n\n        //------------------------------------------------------","sourceCodeStart":836,"sourceCodeEnd":872,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/GeneralTransformCollection.cs#L836-L872","documentation":"GeneralTransformCollection.Enumerator.Current throws this InvalidOperationException when the enumerator has already walked past the end of the collection (internal _index == -2, set when MoveNext() returned false). Per the IEnumerator.Current contract, reading Current after exhaustion must throw rather than return the last item or default.","triggerScenarios":"Accessing Current after MoveNext() has returned false (collection fully consumed), then calling MoveNext() again (which keeps _index at -2) and reading Current, or reading Current repeatedly after the loop ended.","commonSituations":"Manual while(e.MoveNext()) loops that read e.Current after the loop body; code that stores the enumerator and reads Current later assuming it keeps the last value; loops with a trailing Current read outside the MoveNext()==true check.","solutions":["Read Current only inside the loop guarded by a true result from MoveNext().","Keep a local variable holding the last valid item instead of re-reading Current after iteration finishes.","Replace manual enumerator code with foreach, which never reads Current after the end.","If a re-read is needed, call Reset() then MoveNext() to reposition before accessing Current."],"exampleFix":"// before\nwhile (e.MoveNext()) { }\nvar last = e.Current; // throws: past the end\n\n// after\nobject last = null;\nwhile (e.MoveNext()) { last = e.Current; }","handlingStrategy":"validation","validationCode":"// keep the last valid item while iterating\nT last = default;\nwhile (enumerator.MoveNext()) { last = enumerator.Current; }","typeGuard":"static bool TryGetCurrent<T>(this IEnumerator<T> e, out T current) { bool ok = e.MoveNext(); current = ok ? e.Current : default; return ok; }","tryCatchPattern":"try { var v = enumerator.Current; }\ncatch (InvalidOperationException) { /* enumerator exhausted: capture items during iteration instead */ }","preventionTips":["Never read Current after MoveNext() returned false.","Capture the desired item in a local variable inside the loop.","Use foreach, which guarantees Current is only read on valid positions.","If the last item is needed, track it during iteration rather than re-reading."],"tags":["wpf","enumerator","ienumerable","invalidoperation"],"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"}