{"record":{"id":"5632843339d047c0","repo":"dotnet/wpf","slug":"sr-enumerator-notstarted-textdecorationcollection","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/Generated/TextDecorationCollection.cs","lineNumber":840,"sourceCode":"            /// <summary>\n            /// Current element\n            ///\n            /// The behavior of IEnumerable&lt;T>.Current is undefined\n            /// before the first MoveNext and after we have walked\n            /// off the end of the list. However, the IEnumerable.Current\n            /// contract requires that we throw exceptions\n            /// </summary>\n            public TextDecoration 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 TextDecoration _current;\n            private TextDecorationCollection _list;\n            private uint _version;\n            private int _index;\n            #endregion\n        }","sourceCodeStart":822,"sourceCodeEnd":858,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs#L822-L858","documentation":"The enumerator's Current property throws InvalidOperationException (SR.Enumerator_NotStarted) when accessed before the first MoveNext call (_index == -1). Current is only valid between a successful MoveNext and the end of enumeration.","triggerScenarios":"Reading Current immediately after GetEnumerator() without calling MoveNext first; accessing Current inside a finally/cleanup block before the loop ran.","commonSituations":"Manual enumerator code (while loops) forgetting the initial MoveNext; refactored foreach code that still touches enumerator.Current; generic enumerator wrappers that read Current unconditionally.","solutions":["Call MoveNext() and check it returned true before reading Current","Restructure as foreach so the compiler manages the enumerator","Guard reads with a position check (only read Current after a true MoveNext)"],"exampleFix":"// before\nvar e = collection.GetEnumerator();\nvar first = e.Current;\n// after\nvar e = collection.GetEnumerator();\nif (e.MoveNext()) { var first = e.Current; }","handlingStrategy":"validation","validationCode":"// only read Current after a successful MoveNext\nif (enumerator.MoveNext()) { var item = enumerator.Current; }","typeGuard":null,"tryCatchPattern":"InvalidOperationException ex -> ensure a MoveNext() call precedes any Current read; restructure to foreach","preventionTips":["Always call MoveNext() before reading enumerator.Current","Prefer foreach over manual enumerator loops","Initialize reads inside 'if (MoveNext())' blocks"],"tags":["wpf","invalidoperationexception","enumerator","current"],"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"}