dotnet/wpf · error · ArgumentOutOfRangeException
SR.Format(SR.CodePointOutOfRange, key)
Error message
SR.Format(SR.CodePointOutOfRange, key)
What it means
CharacterMetricsDictionary keys are Unicode scalar code points limited to 0..LastDeviceFontCharacterCode (0xFFFF range used by the device-font glyph map). SetValue throws ArgumentOutOfRangeException when a key is negative or above this maximum.
Solutions
- Clamp or validate the key to the valid code-point range before calling Add/indexer.
- Use (int)someChar or codePoint values in 0..0xFFFF for BMP characters.
- For supplementary-plane characters, operate on their UTF-16 code units (surrogate pairs) instead.
Example fix
// before
dict[0x1F600] = metrics; // astral-plane code point
// after
if (codePoint >= 0 && codePoint <= char.MaxValue)
dict[codePoint] = metrics; Defensive patterns
Strategy: validation
Validate before calling
if (key < 0 || key > char.MaxValue)
throw new ArgumentOutOfRangeException(nameof(key), key, "Key must be a valid BMP code point.");
dict[key] = value; Try / catch
try { dict.Add(key, value); }
catch (ArgumentOutOfRangeException ex) { /* log invalid code point and skip */ } Prevention
- Validate keys are non-negative and within the device-font code-point range.
- Don't feed astral-plane (surrogate) code points as single keys.
- Distinguish glyph IDs from Unicode code points.
When it happens
Trigger: Calling Add(key, value) or the indexer this[int key] = value with key < 0 or key > LastDeviceFontCharacterCode (0x10FFFF-ish device-font bound; practically any negative or surrogate-plane value).
Common situations: Feeding full UTF-32 code points from supplementary planes (key > 0xFFFF); sign errors turning a char arithmetic result negative; using character counts or glyph IDs as keys instead of code points.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Argument out of range (end not contained in view)
- Argument out of range (position does not map to a line)
- Argument out of range (start not contained in view)
- ArgumentException.NonNegativeValue (Parameter 'squareScale')
- ArgumentException: path
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/6ae439d08896baa9.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs:355
page = _pageTable[i];
if (page == null)
{
_pageTable[i] = page = new CharacterMetrics[PageSize];
}
}
else
{
_pageTable = new CharacterMetrics[PageCount][];
_pageTable[i] = page = new CharacterMetrics[PageSize];
}
return page;
}
private void SetValue(int key, CharacterMetrics value, bool failIfExists)
{
if (key < 0 || key > LastDeviceFontCharacterCode)
throw new ArgumentOutOfRangeException(SR.Format(SR.CodePointOutOfRange, key));
ArgumentNullException.ThrowIfNull(value);
CharacterMetrics[] page = GetPageFromUnicodeScalar(key);
int i = key & PageMask;
if (failIfExists && page[i] != null)
throw new ArgumentException(SR.Format(SR.CollectionDuplicateKey, key));
page[i] = value;
_count = 0;
}
internal CharacterMetrics GetValue(int key)
{
CharacterMetrics metrics = null;
if (key >= 0 && key <= FontFamilyMap.LastUnicodeScalar && _pageTable != null)View on GitHub (pinned to 81131a70a4)