{"record":{"id":"ef521bff7575b7d5","repo":"dotnet/wpf","slug":"sr-enumerator-verifycontext-culturespecificstringdictionary","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/CultureSpecificStringDictionary.cs","lineNumber":393,"sourceCode":"            // classes to return the Key and Value, respectively.\n            public virtual object Current\n            {\n                get\n                {\n                    return Entry;\n                }\n            }\n\n            private KeyValuePair<XmlLanguage, string> GetCurrentEntry()\n            {\n                // Get the key-value pair from the generic IDictionary.\n                KeyValuePair<XmlLanguage, string> entry = _enumerator.Current;\n\n                // If there is no current item a non-generic IEnumerator should throw an exception,\n                // but a generic IEnumerator<T> is not required to. Therefore we need to check for\n                // this case here by checking for a null Key.\n                if (entry.Key == null)\n                    throw new InvalidOperationException(SR.Enumerator_VerifyContext);\n\n                return entry;\n            }\n\n            public SC.DictionaryEntry Entry\n            {\n                get\n                {\n                    KeyValuePair<XmlLanguage, string> entry = GetCurrentEntry();\n                    return new SC.DictionaryEntry(entry.Key, entry.Value);\n                }\n            }\n\n            public object Key\n            {\n                get\n                {\n                    return GetCurrentEntry().Key;","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CultureSpecificStringDictionary.cs#L375-L411","documentation":"The non-generic enumerator wrapper of CultureSpecificStringDictionary throws InvalidOperationException (Enumerator_VerifyContext) when Current is read while no current item exists. Generic IEnumerator<T> is not required to throw in that case, so the IDictionaryEntry-producing wrapper detects a null Key and surfaces the standard 'enumerator out of context' error.","triggerScenarios":"Calling entry.Key, entry.Value, or entry.Entry on the non-generic IDictionaryEnumerator positioned before MoveNext, after MoveNext returned false, or after the collection was modified (invalidation).","commonSituations":"Manual foreach over the non-generic IDictionary interface (e.g. from data-binding or scripting engines); reading the current entry before the first MoveNext; collection mutated during enumeration by another thread.","solutions":["Call MoveNext() and check it returned true before reading Key/Value/Entry.","Restart enumeration with GetEnumerator() after the collection changes.","Enumerate the generic IEnumerable<KeyValuePair<XmlLanguage,string>> surface or use ToArray() to snapshot."],"exampleFix":"// before\nvar e = ((IDictionary)dict).GetEnumerator();\nvar key = e.Key; // no MoveNext yet\n// after\nvar e = ((IDictionary)dict).GetEnumerator();\nif (e.MoveNext()) { var key = e.Key; }","handlingStrategy":"validation","validationCode":"if (!enumerator.MoveNext()) return; // no current entry\nvar key = enumerator.Key; // safe now","typeGuard":"static bool HasCurrent(IDictionaryEnumerator e) { try { return e.Key != null; } catch (InvalidOperationException) { return false; } }","tryCatchPattern":"try { var k = enumerator.Key; }\ncatch (InvalidOperationException) { /* enumerator not positioned; call MoveNext first */ }","preventionTips":["Only read Key/Value/Entry after MoveNext() returned true.","Re-get the enumerator after any collection modification.","Prefer generic foreach over KeyValuePair<XmlLanguage,string> to avoid the non-generic wrapper entirely."],"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-21T21:30:21.729Z"}