{"record":{"id":"0b74387b7f90426b","repo":"dotnet/wpf","slug":"sr-enumeratornotstarted-grid","errorCode":null,"errorMessage":"SR.EnumeratorNotStarted","messagePattern":"SR\\.EnumeratorNotStarted","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Grid.cs","lineNumber":3971,"sourceCode":"                                          _enumerator2Index++;\n                                          return (true);\n                                      }\n                                      break;\n                        }\n                    }\n                    _currentEnumerator++;\n                }\n                return (false);\n            }\n\n            public Object Current\n            {\n                get\n                {\n                    if (_currentEnumerator == -1)\n                    {\n                        // IEnumerator.Current is documented to throw this exception\n                        throw new InvalidOperationException(SR.EnumeratorNotStarted);\n                    }\n                    if (_currentEnumerator >= 3)\n                    {\n                        // IEnumerator.Current is documented to throw this exception\n                        throw new InvalidOperationException(SR.EnumeratorReachedEnd);\n                    }\n\n                    //  assert below is not true anymore since UIElementCollection allowes for null children\n                    //Debug.Assert(_currentChild != null);\n                    return (_currentChild);\n                }\n            }\n\n            public void Reset()\n            {\n                _currentEnumerator = -1;\n                _currentChild = null;\n                _enumerator0.Reset();","sourceCodeStart":3953,"sourceCodeEnd":3989,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Grid.cs#L3953-L3989","documentation":"Grid's child enumerator's Current property throws InvalidOperationException (SR.EnumeratorNotStarted) when Current is read before the first MoveNext() call (_currentEnumerator == -1). This mirrors the documented IEnumerator.Current contract: accessing Current before enumeration starts is invalid.","triggerScenarios":"Reading foreach-free enumerator.Current immediately after GetEnumerator() without a prior MoveNext(); manually driving IEnumerator over Grid children and dereferencing Current first.","commonSituations":"Hand-written enumeration loops over Grid/UIElementCollection where Current is read before MoveNext; refactored foreach loops converted to explicit enumerator calls; LINQ or custom iterators misusing the enumerator.","solutions":["Call MoveNext() before reading Current — or simply use a foreach loop, which handles this correctly.","Check MoveNext()'s return value before accessing Current inside the loop body.","Reset/initialize the enumerator if reusing it rather than reading Current on a fresh instance."],"exampleFix":"// before\nvar e = grid.Children.GetEnumerator();\nvar first = e.Current; // InvalidOperationException\n// after\nvar e = grid.Children.GetEnumerator();\nwhile (e.MoveNext())\n{\n    var child = e.Current;\n}","handlingStrategy":"validation","validationCode":"var e = grid.Children.GetEnumerator(); bool started = e.MoveNext();","typeGuard":"bool CanReadCurrent(IEnumerator e) => e.MoveNext(); // true means Current is now valid","tryCatchPattern":"try { var cur = e.Current; }\ncatch (InvalidOperationException) { /* Current read before MoveNext; start the enumeration */ }","preventionTips":["Prefer foreach over manual enumerators","Always call MoveNext before reading Current","Check MoveNext's return value before use"],"tags":["wpf","grid","enumerator","ienumerable","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-22T01:17:13.364Z"}