{"record":{"id":"88ce78b32a16c150","repo":"dotnet/wpf","slug":"sr-enumerator-verifycontext","errorCode":null,"errorMessage":"SR.Enumerator_VerifyContext","messagePattern":"SR\\.Enumerator_VerifyContext","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs","lineNumber":576,"sourceCode":"                    return new SC.DictionaryEntry(entry.Key, entry.Value);\n                }\n            }\n\n            // Current property for generic enumerator.\n            public KeyValuePair<int, CharacterMetrics> Current\n            {\n                get\n                {\n                    return new KeyValuePair<int, CharacterMetrics>(_unicodeScalar, _value);\n                }\n            }\n\n            private KeyValuePair<int, CharacterMetrics> GetCurrentEntry()\n            {\n                if (_value != null)\n                    return new KeyValuePair<int, CharacterMetrics>(_unicodeScalar, _value);\n                else\n                    throw new InvalidOperationException(SR.Enumerator_VerifyContext);\n            }\n\n            SC.DictionaryEntry SC.IDictionaryEnumerator.Entry\n            {\n                get\n                {\n                    KeyValuePair<int, CharacterMetrics> entry = GetCurrentEntry();\n                    return new SC.DictionaryEntry(entry.Key, entry.Value);\n                }\n            }\n\n            object SC.IDictionaryEnumerator.Key\n            {\n                get\n                {\n                    return GetCurrentEntry().Key;\n                }\n            }","sourceCodeStart":558,"sourceCodeEnd":594,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs#L558-L594","documentation":"The CharacterMetricsDictionary Enumerator's GetCurrentEntry throws this InvalidOperationException when Entry or Current is accessed before the enumerator has successfully advanced to a valid element (MoveNext was never called or returned false). SR.Enumerator_VerifyContext means there is no current entry to return.","triggerScenarios":"Accessing IDictionaryEnumerator.Entry, Entry.Key/Value, or the KeyValuePair Current immediately after GetEnumerator(), after Reset(), or after MoveNext() returned false (end of collection or empty dictionary).","commonSituations":"Enumerating an empty CharacterMetricsDictionary and reading Current unconditionally; manual enumerator loops that read Entry before the first MoveNext; iterators that use IDictionaryEnumerator.Entry without guarding.","solutions":["Call MoveNext() and check it returns true before reading Entry/Current","Guard the Entry access with a check that enumeration has started and is positioned on a valid element","Handle empty dictionaries explicitly before enumerating"],"exampleFix":"// before\n var e = ((IDictionary)dict).GetEnumerator();\n var entry = ((IDictionaryEnumerator)e).Entry;\n// after\n var e = ((IDictionary)dict).GetEnumerator();\n if (e.MoveNext()) { var entry = ((IDictionaryEnumerator)e).Entry; }","handlingStrategy":"try-catch","validationCode":"bool CanReadEntry(IEnumerator e) => e is IDictionaryEnumerator de && /* enumeration started and positioned */ de.MoveNextCalled(); // track MoveNext yourself","typeGuard":null,"tryCatchPattern":"try { var entry = ((IDictionaryEnumerator)enumerator).Entry; } catch (InvalidOperationException) { /* enumerator not positioned; call MoveNext first */ }","preventionTips":["Always call MoveNext() and check true before reading Entry/Current","Never read Current on a fresh or Reset enumerator","Handle empty dictionaries before iterating"],"tags":["wpf","enumerator","invalid-operation","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"}