{"record":{"id":"c38f2ae16eaa83e4","repo":"dotnet/wpf","slug":"invalidoperationexception-emptyenumerator","errorCode":null,"errorMessage":"InvalidOperationException","messagePattern":"InvalidOperationException","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/EmptyEnumerator.cs","lineNumber":55,"sourceCode":"        /// <summary>\n        /// Does nothing.\n        /// </summary>\n        public void Reset() { }\n\n        /// <summary>\n        /// Returns false.\n        /// </summary>\n        /// <returns>false</returns>\n        public bool MoveNext() { return false; }\n\n        /// <summary>\n        /// Returns null.\n        /// </summary>\n        public object Current\n        {\n            get\n            {\n                throw new InvalidOperationException();\n            }\n        }\n\n        private static IEnumerator _instance;\n    }\n}\n","sourceCodeStart":37,"sourceCodeEnd":62,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/EmptyEnumerator.cs#L37-L62","documentation":"EmptyEnumerator is a singleton IEnumerator over zero items. Its Current getter always throws InvalidOperationException because there is no current element; callers must only use it after a false MoveNext/Reset contract — meaning Current should never legitimately be called.","triggerScenarios":"Accessing the Current property of an empty collection's enumerator without a successful prior MoveNext(), or enumerating an empty custom collection exposed by WPF internals and reading Current defensively.","commonSituations":"Hand-rolled enumeration loops that read Current before/after MoveNext; code that assumes MoveNext returned true; reflection/debug tooling inspecting empty collections.","solutions":["Only access Current when MoveNext() returned true","Use foreach instead of manual enumerator handling","Check collection count before enumerating Current manually"],"exampleFix":"// before\nvar e = col.GetEnumerator(); var first = e.Current;\n// after\nvar e = col.GetEnumerator();\nif (e.MoveNext()) { var first = e.Current; }","handlingStrategy":"type-guard","validationCode":"bool HasCurrent(IEnumerator e) => e.MoveNext(); // only then read Current","typeGuard":"bool TryGetCurrent(IEnumerator e, out object value) { value = null; if (e.MoveNext()) { value = e.Current; return true; } return false; }","tryCatchPattern":"try { var v = e.Current; } catch (InvalidOperationException) { /* empty enumerator; Current is undefined */ }","preventionTips":["Use foreach instead of manual enumerator access","Only read Current after a true MoveNext","Never assume non-empty collections"],"tags":["wpf","enumerator","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"}