{"record":{"id":"b13fc7248c5a20e9","repo":"dotnet/wpf","slug":"sr-enumerator-notstarted-visualcollection","errorCode":null,"errorMessage":"SR.Enumerator_NotStarted","messagePattern":"SR\\.Enumerator_NotStarted","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs","lineNumber":947,"sourceCode":"                get\n                {\n                    return this.Current;\n                }\n            }\n\n            /// <summary>\n            /// Gets the current Visual.\n            /// </summary>\n            public Visual Current\n            {\n                get\n                {\n                    if (_index < 0)\n                    {\n                        if (_index == -1)\n                        {\n                            // Not started.\n                            throw new InvalidOperationException(SR.Enumerator_NotStarted);\n                        }\n                        else\n                        {\n                            // Reached the end.\n                            Debug.Assert(_index == -2);\n                            throw new InvalidOperationException(SR.Enumerator_ReachedEnd);\n                        }\n                    }\n\n                    return _currentElement;\n                }\n            }\n\n\n            /// <summary>\n            /// Sets the enumerator to its initial position, which is before the first element in the collection.\n            /// </summary>\n            public void Reset()","sourceCodeStart":929,"sourceCodeEnd":965,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs#L929-L965","documentation":"The VisualCollection Enumerator's Current property throws InvalidOperationException(SR.Enumerator_NotStarted) when MoveNext has never been called before accessing Current. The enumerator's _index is still -1, so there is no current element to return.","triggerScenarios":"Creating an enumerator via VisualCollection.GetEnumerator() and reading Current (or the foreach iteration variable pattern manually) before the first MoveNext() call.","commonSituations":"Hand-written enumerator loops that read Current in an initialization step; custom code that stores enumerator.Current outside a foreach; foreach is safe — only manual enumerator use hits this.","solutions":["Call MoveNext() at least once and check its return value before reading Current","Prefer foreach over manual enumerator usage, which enforces this ordering automatically","Restructure manual loops to follow the standard while (enumerator.MoveNext()) { use Current } pattern"],"exampleFix":"// before\nvar e = visualCollection.GetEnumerator();\nvar first = e.Current; // throws: not started\n// after\nvar e = visualCollection.GetEnumerator();\nif (e.MoveNext()) { var first = e.Current; }","handlingStrategy":"try-catch","validationCode":"// no pre-call validation possible; use foreach or check via enumerator state","typeGuard":null,"tryCatchPattern":"try { var cur = e.Current; }\ncatch (InvalidOperationException) { /* MoveNext not yet called; call it first */ }","preventionTips":["Prefer foreach, which never reads Current before MoveNext","In manual loops, always branch on MoveNext()'s return before Current","Do not read Current outside the iteration body"],"tags":["wpf","enumerator","invalid-operation"],"backgroundTag":"enumerator-current-before-movenext","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"}