{"record":{"id":"9eaa965d3bd1fcfd","repo":"dotnet/wpf","slug":"sr-format-sr-collectionduplicatekey-key","errorCode":null,"errorMessage":"SR.Format(SR.CollectionDuplicateKey, key)","messagePattern":"SR\\.Format\\(SR\\.CollectionDuplicateKey, key\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs","lineNumber":363,"sourceCode":"                _pageTable = new CharacterMetrics[PageCount][];\n                _pageTable[i] = page = new CharacterMetrics[PageSize];\n            }\n\n            return page;\n        }\n\n        private void SetValue(int key, CharacterMetrics value, bool failIfExists)\n        {\n            if (key < 0 || key > LastDeviceFontCharacterCode)\n                throw new ArgumentOutOfRangeException(SR.Format(SR.CodePointOutOfRange, key));\n\n            ArgumentNullException.ThrowIfNull(value);\n\n            CharacterMetrics[] page = GetPageFromUnicodeScalar(key);\n            int i = key & PageMask;\n\n            if (failIfExists && page[i] != null)\n                throw new ArgumentException(SR.Format(SR.CollectionDuplicateKey, key));\n\n            page[i] = value;\n            _count = 0;\n        }\n\n        internal CharacterMetrics GetValue(int key)\n        {\n            CharacterMetrics metrics = null;\n\n            if (key >= 0 && key <= FontFamilyMap.LastUnicodeScalar && _pageTable != null)\n            {\n                CharacterMetrics[] page = _pageTable[key >> PageShift];\n                if (page != null)\n                    metrics = page[key & PageMask];\n            }\n\n            return metrics;\n        }","sourceCodeStart":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs#L345-L381","documentation":"CharacterMetricsDictionary.Add(key, value) must not overwrite an existing entry. SetValue is invoked with failIfExists=true from Add; if a CharacterMetrics already exists for that code point, an ArgumentException (CollectionDuplicateKey) is thrown. (The indexer uses failIfExists=false and silently overwrites instead.)","triggerScenarios":"Calling dictionary.Add(key, value) when key already has an entry — e.g. adding metrics for the same code point twice, or re-running an initialization pass without clearing the dictionary.","commonSituations":"Populating the dictionary from a font metrics table that contains duplicate code points; idempotency bugs where setup code runs twice; assuming Add behaves like the indexer.","solutions":["Check dictionary.ContainsKey(key) (or the indexer != null) before calling Add.","Use the indexer assignment dict[key] = value if overwrite is intended.","Deduplicate the source metrics table by code point before populating."],"exampleFix":"// before\ndict[0x41] = metricsA;\ndict.Add(0x41, metricsB); // throws\n// after\nif (!dict.ContainsKey(0x41)) dict.Add(0x41, metricsB);","handlingStrategy":"validation","validationCode":"if (!dict.ContainsKey(key))\n    dict.Add(key, value);","typeGuard":null,"tryCatchPattern":"try { dict.Add(key, value); }\ncatch (ArgumentException ex) { /* key already present; overwrite or log */ }","preventionTips":["Use the indexer dict[key] = value when overwrite is intended.","Check ContainsKey before Add.","Deduplicate code points in source metrics tables before population."],"tags":["wpf","dictionary","duplicate-key","argument-exception"],"backgroundTag":"conflicting-config-options","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"}