{"record":{"id":"947ea3940dcf0d37","repo":"dotnet/wpf","slug":"sr-enumerator-notstarted","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/FreezableCollection.cs","lineNumber":1074,"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 T 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 T _current;\n            private FreezableCollection<T> _list;\n            private uint _version;\n            private int _index;\n            #endregion\n        }","sourceCodeStart":1056,"sourceCodeEnd":1092,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FreezableCollection.cs#L1056-L1092","documentation":"FreezableCollection<T>.Enumerator.Current throws InvalidOperationException(SR.Enumerator_NotStarted) when accessed before the first MoveNext call (_index == -1). There is no current element yet, so reading Current is undefined and rejected.","triggerScenarios":"Reading enumerator.Current (or using foreach's variable) before calling MoveNext, e.g. accessing Current immediately after GetEnumerator() or accessing the loop variable before the first iteration in hand-rolled loops.","commonSituations":"Hand-rolled enumeration code that reads Current before advancing, refactored foreach loops where an early 'peek' at Current was added, storing the enumerator and reading Current later before starting.","solutions":["Call MoveNext() at least once and check it returned true before reading Current.","Prefer foreach, which guarantees Current is only read after a successful MoveNext.","Initialize/prime the enumerator before exposing it to code that reads Current."],"exampleFix":"// before\nvar e = collection.GetEnumerator();\nvar first = e.Current; // throws\n// after\nvar e = collection.GetEnumerator();\nif (e.MoveNext()) { var first = e.Current; }","handlingStrategy":"validation","validationCode":"if (!started) throw new InvalidOperationException(\"Call MoveNext() before reading Current.\");","typeGuard":"bool TryGetFirst<T>(IEnumerator<T> e, out T value) { if (e.MoveNext()) { value = e.Current; return true; } value = default; return false; }","tryCatchPattern":"try { var cur = e.Current; }\ncatch (InvalidOperationException) { /* enumerator not started; call MoveNext first */ }","preventionTips":["Always check MoveNext() returned true before reading Current","Prefer foreach which handles this ordering","Never read Current immediately after GetEnumerator()"],"tags":["wpf","enumerator","invalid-state","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"}