{"record":{"id":"0325d6300cd881a4","repo":"dotnet/wpf","slug":"sr-localvalueenumerationoutofbounds","errorCode":null,"errorMessage":"SR.LocalValueEnumerationOutOfBounds","messagePattern":"SR\\.LocalValueEnumerationOutOfBounds","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/LocalValueEnumerator.cs","lineNumber":76,"sourceCode":"        }\n        \n        /// <summary>\n        ///     Get current entry\n        /// </summary>\n        public LocalValueEntry Current\n        {\n            get\n            {\n                if(_index == -1 )\n                {\n                    // IEnumerator.Current is documented to throw this exception\n                    throw new InvalidOperationException(SR.LocalValueEnumerationReset);\n                }\n\n                if(_index >= Count )\n                {\n                    // IEnumerator.Current is documented to throw this exception\n                    throw new InvalidOperationException(SR.LocalValueEnumerationOutOfBounds);\n                }\n                \n                return _snapshot[_index];\n            }\n        }\n\n        /// <summary>\n        ///     Get current entry (object reference based)\n        /// </summary>\n        object IEnumerator.Current\n        {\n            get { return Current; }\n        }\n\n        /// <summary>\n        ///     Move to the next item in the enumerator\n        /// </summary>\n        /// <returns>Success of the method</returns>","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/LocalValueEnumerator.cs#L58-L94","documentation":"LocalValueEnumerator.Current throws InvalidOperationException(SR.LocalValueEnumerationOutOfBounds) when the enumerator has moved past the last element (MoveNext returned false and iteration ended). This implements the documented IEnumerator.Current behavior. Reading Current after enumeration has completed is a caller sequencing error.","triggerScenarios":"Continuing to read Current after MoveNext() has returned false; calling Current again after the loop finished; reusing a spent enumerator.","commonSituations":"do/while loops that read Current before checking MoveNext's return value; caching the enumerator and dereferencing Current later; custom collection processors that run one iteration too many.","solutions":["Only access Current when MoveNext() returns true.","Replace manual enumerator code with a foreach loop.","Reset the enumerator via Reset() if you need to iterate again (and then MoveNext first).","Capture values inside the loop instead of dereferencing Current afterwards."],"exampleFix":"// before\nvar e = obj.GetLocalValueEnumerator();\nwhile (e.MoveNext()) { /* ... */ }\nvar last = e.Current; // throws: past end\n// after\nvar e = obj.GetLocalValueEnumerator();\nLocalValueEntry last = null;\nwhile (e.MoveNext()) { last = e.Current; }","handlingStrategy":"validation","validationCode":"// read Current only inside: while (e.MoveNext()) { use e.Current; }","typeGuard":"null","tryCatchPattern":"try { var cur = e.Current; }\ncatch (InvalidOperationException) { /* enumeration already finished */ }","preventionTips":["Check MoveNext()'s return value before every Current access.","Use foreach to eliminate manual sequencing bugs.","Do not reuse spent enumerators; create a new one via GetLocalValueEnumerator()."],"tags":["wpf","enumerator","invalid-operation"],"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"}