{"record":{"id":"9d3850f0469db852","repo":"dotnet/wpf","slug":"sr-enumerator-notstarted-materialcollection","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/Media3D/Generated/MaterialCollection.cs","lineNumber":882,"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 Material 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 Material _current;\n            private MaterialCollection _list;\n            private uint _version;\n            private int _index;\n            #endregion\n        }","sourceCodeStart":864,"sourceCodeEnd":900,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/MaterialCollection.cs#L864-L900","documentation":"The enumerator's Current property tracks position via _index: -1 means MoveNext() has never been called, so no current element exists and it throws InvalidOperationException(SR.Enumerator_NotStarted). .NET enumerators must be advanced before Current is valid; MaterialCollection implements this fail-fast behavior explicitly.","triggerScenarios":"Reading enumerator.Current (or calling Current manually on IEnumerator) immediately after GetEnumerator() without a prior MoveNext(), e.g. var e = materials.GetEnumerator(); var m = e.Current;","commonSituations":"Hand-rolled iteration code that skips the initial MoveNext; code translated from languages where iterators point at the first element before advancing; peeking at the 'first' item directly.","solutions":["Call MoveNext() before reading Current: if (e.MoveNext()) { var m = e.Current; }","Prefer foreach, which handles MoveNext/Current sequencing correctly.","If you need the first element without an enumerator, use materials[0] after checking Count > 0."],"exampleFix":"// before\nvar e = materials.GetEnumerator();\nvar first = e.Current;\n// after\nvar e = materials.GetEnumerator();\nvar first = e.MoveNext() ? e.Current : null;","handlingStrategy":"type-guard","validationCode":"bool hasCurrent = enumerator.MoveNext(); // must be true before reading Current","typeGuard":"static bool TryPeekFirst<T>(IEnumerator<T> e, out T current) { if (e.MoveNext()) { current = e.Current; return true; } current = default; return false; }","tryCatchPattern":"try { var m = e.Current; }\ncatch (InvalidOperationException) { /* enumerator not started; call MoveNext first */ }","preventionTips":["Always pair Current with a successful MoveNext()","Prefer foreach over manual enumerator code","Use the indexer (materials[0]) for direct first-element access"],"tags":["wpf","enumerator","invalid-state"],"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"}