dotnet/wpf · error · ArgumentException
SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength
Error message
SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength
What it means
CharacterMetricsDictionary.CopyTo(KeyValuePair<int,CharacterMetrics>[] array, int index) validates the destination index. If index is greater than or equal to array.Length, no element can ever be written and an ArgumentException is thrown. (A null array or negative index fails earlier with different exceptions.)
Solutions
- Pass an index strictly less than array.Length (and >= 0).
- Ensure the array is large enough: index + Count <= array.Length.
- Guard the call with `if (index < array.Length)` before invoking CopyTo.
Example fix
// before dict.CopyTo(pairs, pairs.Length); // after if (pairs.Length > 0) dict.CopyTo(pairs, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (array == null) throw new ArgumentNullException(nameof(array));
if (index < 0 || index >= array.Length)
throw new ArgumentOutOfRangeException(nameof(index)); Try / catch
try { dict.CopyTo(array, index); }
catch (ArgumentException ex) { /* fix index/array sizing, retry */ } Prevention
- Assert index < array.Length before CopyTo.
- Prefer copying into a right-sized array at index 0.
- Beware empty arrays with any index.
When it happens
Trigger: Calling dictionary.CopyTo(array, index) where index >= array.Length, e.g. CopyTo(new KeyValuePair<int,CharacterMetrics>[10], 10) or CopyTo(someArray, someArray.Length).
Common situations: Computing a start index from a cursor that has advanced past the buffer; off-by-one when copying into the tail of a larger array; passing an empty array with index 0.
Related errors
- Collection_CopyTo_ArrayCannotBeMultidimensional
- Collection_CopyTo_ArrayCannotBeMultidimensional
- Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength
- Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength
- Collection_CopyTo_NumberOfElementsExceedsArrayLength
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/bc0d346d9e24b506.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs:121
/// </summary>
[CLSCompliant(false)]
public bool Contains(KeyValuePair<int, CharacterMetrics> item)
{
return item.Value != null && item.Value.Equals(GetValue(item.Key));
}
/// <summary>
/// Copies the contents of the collection to the specified array.
/// </summary>
[CLSCompliant(false)]
public void CopyTo(KeyValuePair<int, CharacterMetrics>[] array, int index)
{
ArgumentNullException.ThrowIfNull(array);
ArgumentOutOfRangeException.ThrowIfNegative(index);
if (index >= array.Length)
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array"));
CharacterMetrics[][] pageTable = _pageTable;
if (pageTable != null)
{
int k = index;
for (int i = 0; i < pageTable.Length; ++i)
{
CharacterMetrics[] page = pageTable[i];
if (page != null)
{
for (int j = 0; j < page.Length; ++j)
{
CharacterMetrics metrics = page[j];
if (metrics != null)
{
if (k >= array.Length)
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, "array"));View on GitHub (pinned to 81131a70a4)