{"record":{"id":"ed03db11acf2b57a","repo":"dotnet/wpf","slug":"sr-parserkeysarestrings-dictionary-keys-must-be-strings","errorCode":null,"errorMessage":"SR.ParserKeysAreStrings (dictionary keys must be strings)","messagePattern":"SR\\.ParserKeysAreStrings \\(dictionary keys must be strings\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs","lineNumber":470,"sourceCode":"        /// <summary>\n        /// A property indexing into the dictionary by XML prefix\n        /// </summary>\n        public string this[string prefix]\n        {\n            get {   return LookupNamespace(prefix); }\n            set {   AddNamespace(prefix, value as string);}\n        }\n\n        /// <summary>\n        /// A property indexing into the dictionary by XML prefix (supports objects to satisfy IDictionary spec)\n        /// </summary>\n        public object this[object prefix]\n        {\n            get \n            {\n                if (!(prefix is string))\n                {\n                    throw new ArgumentException(SR.ParserKeysAreStrings);\n                }\n                return LookupNamespace((string)prefix); \n            }\n            set \n            {   \n                if (!(prefix is string) || !(value is string))\n                {\n                    throw new ArgumentException(SR.ParserKeysAreStrings);\n                }\n                AddNamespace((string)prefix, (string)value); \n            }\n        }\n\n        /// <summary>\n        /// An ICollection of all keys in the dictionary\n        /// </summary>\n        public ICollection Keys\n        {","sourceCodeStart":452,"sourceCodeEnd":488,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs#L452-L488","documentation":"The object indexer this[object prefix] throws this ArgumentException when the key is not a string. XmlnsDictionary is keyed by XML namespace prefixes, which are strings; the IDictionary object-based accessor enforces this by rejecting non-string keys (and null values on set) with the ParserKeysAreStrings message.","triggerScenarios":"Reading dictionary[nonStringKey], e.g. dictionary[42] or dictionary[someUri].","commonSituations":"Generic code iterating IDictionary keys of mixed types; using a Uri object as the key instead of its string representation.","solutions":["Cast the key to string (or call key.ToString()) before indexing","Verify key type with 'is string' before lookup"],"exampleFix":"// before\nvar ns = dictionary[key]; // key is object\n\n// after\nvar ns = key is string s ? dictionary[s] : null;","handlingStrategy":"type-guard","validationCode":"if (key is string p) { var ns = dictionary[p]; }","typeGuard":"bool IsStringKey(object key) => key is string;","tryCatchPattern":"try { var ns = dictionary[key]; }\ncatch (ArgumentException) { /* key was not a string */ }","preventionTips":["Always index XmlnsDictionary with string keys","Convert Uri/int keys to strings before indexing","Restrict generic dictionary-walking code from touching XmlnsDictionary untyped"],"tags":["wpf","xaml","type-mismatch","indexer"],"backgroundTag":"type-mismatch","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"}