{"record":{"id":"7f4377f50716ed68","repo":"dotnet/wpf","slug":"sr-format-sr-cannotconvertstringtotype-s-int","errorCode":null,"errorMessage":"SR.Format(SR.CannotConvertStringToType, s, \"int\")","messagePattern":"SR\\.Format\\(SR\\.CannotConvertStringToType, s, \"int\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs","lineNumber":492,"sourceCode":"            foreach (KeyValuePair<int, CharacterMetrics> pair in this)\n            {\n                result[i++] = pair.Value;\n            }\n            return result;\n        }\n\n        internal static int ConvertKey(object key)\n        {\n            ArgumentNullException.ThrowIfNull(key);\n\n            int value;\n\n            string s = key as string;\n            if (s != null)\n            {\n                int i = 0;\n                if (!FontFamilyMap.ParseHexNumber(s, ref i, out value) || i < s.Length)\n                    throw new ArgumentException(SR.Format(SR.CannotConvertStringToType, s, \"int\"), nameof(key));\n            }\n            else if (key is int)\n            {\n                value = (int)key;\n            }\n            else\n            {\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, key.GetType(), \"int\"), nameof(key));\n            }\n\n            if (value < 0 || value > FontFamilyMap.LastUnicodeScalar)\n                throw new ArgumentException(SR.Format(SR.CodePointOutOfRange, value), nameof(key));\n\n            return value;\n        }\n\n        private CharacterMetrics ConvertValue(object value)\n        {","sourceCodeStart":474,"sourceCodeEnd":510,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs#L474-L510","documentation":"CharacterMetricsDictionary keys must be Unicode code points (int) or hex strings parseable to one. ConvertKey throws this ArgumentException when a string key cannot be fully parsed as a hexadecimal number by FontFamilyMap.ParseHexNumber (e.g. because i < s.Length, meaning trailing garbage remains after the parsed prefix).","triggerScenarios":"Passing a string key to Add(object, object), the IDictionary members, or the indexer whose text is not a complete hex number, such as \"41x\", \"0x41\", or \"\" — the parse either fails or stops before the end of the string.","commonSituations":"Populating the dictionary from config/XML where keys were authored as decimal (\"65\") or C-style hex (\"0x41\") instead of the expected bare hex form (\"41\"); copy-pasted keys with whitespace or trailing characters.","solutions":["Use bare hexadecimal string keys without prefixes or suffixes, e.g. \"41\" instead of \"0x41\"","Pass the key as an int (the Unicode scalar value) instead of a string","Trim the string and verify it parses with int.Parse(s, NumberStyles.HexNumber) before calling Add"],"exampleFix":"// before\n dictionary.Add(\"0x41\", new CharacterMetrics());\n// after\n dictionary.Add(\"41\", new CharacterMetrics()); // or dictionary.Add(0x41, ...)","handlingStrategy":"validation","validationCode":"bool IsValidHexKey(string s) => !string.IsNullOrEmpty(s) && int.TryParse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int v) && v >= 0 && v <= 0x10FFFF;","typeGuard":"bool IsHexKey(object key) => key is int || (key is string s && int.TryParse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out _));","tryCatchPattern":"try { dict.Add(key, value); } catch (ArgumentException ex) when (ex.ParamName == \"key\") { /* log/fix key format */ }","preventionTips":["Author keys as bare hex strings without 0x prefix","Prefer int keys (Unicode scalar values) over strings","Validate keys with int.TryParse(..., NumberStyles.HexNumber) before insertion"],"tags":["wpf","argument","key-format","hex-parsing"],"backgroundTag":"invalid-argument-format","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"}