dotnet/wpf · error · ArgumentException
SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo…
Error message
SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, CodepointCount + 1)
What it means
When caretStops is supplied and non-empty, GlyphRun.Initialize requires caretStops.Count == CodepointCount + 1 (one stop per codepoint plus a trailing stop). Any other count is rejected with ArgumentException on the caretStops parameter.
Solutions
- Size caretStops to CodepointCount + 1 (count surrogate pairs as one codepoint)
- Pass null (or empty) caretStops if default caret behavior is acceptable
- Compute codepoint count with StringInfo.GetTextElementEnumerator / char.IsSurrogatePair logic
Example fix
// before caretStops = new bool[chars.Length]; // after caretStops = new bool[CodepointCount + 1]; // +1 for trailing stop
Defensive patterns
Strategy: validation
Validate before calling
if (caretStops != null && caretStops.Count != 0 && caretStops.Count != CodepointCount(characters) + 1)
throw new ArgumentException("caretStops must have CodepointCount+1 entries"); Type guard
static bool CaretStopsValid(IList<bool> stops, int codepointCount) => stops == null || stops.Count == 0 || stops.Count == codepointCount + 1;
Try / catch
try { var run = new GlyphRun(...); }
catch (ArgumentException ex) when (ex.ParamName == "caretStops") { /* fix size or pass null */ } Prevention
- Size caret stops as codepoints+1, counting surrogate pairs once
- Pass null when default caret behavior suffices
- Unit-test caret stop sizing on astral-plane characters
When it happens
Trigger: Calling the GlyphRun constructor, TryCreate, or EndInit with a caretStops collection whose length is not (number of codepoints + 1) — e.g. sizing it to characters.Count or glyphIndices.Count.
Common situations: Implementing custom caret navigation / hit-testing and miscomputing the caret stop array length, especially with surrogate pairs where codepoints differ from chars.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- SR.ClusterMapEntriesShouldNotDecrease
- SR.ClusterMapEntryShouldPointWithinGlyphIndices
- SR.ClusterMapFirstEntryMustBeZero
- SR.CollectionNumberOfElementsMustBeGreaterThanZero
- SR.Format(SR.CollectionNumberOfElementsMustBeLessOrEqualTo…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/5c6a4cfde4a97df4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs:411
throw new ArgumentException(SR.ClusterMapFirstEntryMustBeZero, nameof(clusterMap));
}
}
else
{
throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, characters.Count), nameof(clusterMap));
}
}
else
{
if (GlyphCount != characters.Count)
throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, GlyphCount), nameof(clusterMap));
}
}
if (caretStops != null && caretStops.Count != 0)
{
if (caretStops.Count != CodepointCount + 1)
throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, CodepointCount + 1), nameof(caretStops));
}
if (isSideways && (bidiLevel & 1) != 0)
throw new ArgumentException(SR.SidewaysRTLTextIsNotSupported);
// NOTE: In previous versions this function would estimate the size
// of this glyph run's bitmaps and compare it against the theoretical
// maximum size allowed before rendering falls back to using geometry.
// This was done in order to produce a managed exception where we might
// hit overflow or memory allocation issues in native code.
// We no longer own the code the produces these bitmaps so we can't reliably
// avoid the issue here any longer.
}
else
{
ArgumentOutOfRangeException.ThrowIfEqual(renderingEmSize, double.NaN);
ArgumentOutOfRangeException.ThrowIfNegative(renderingEmSize);
ArgumentNullException.ThrowIfNull(glyphTypeface);View on GitHub (pinned to 81131a70a4)