dotnet/wpf · error · ArgumentException
SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayL…
Error message
SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, "array")
What it means
CopyItems (invoked from FamilyTypefaceCollection.CopyTo) throws this ArgumentException when the destination array starting at 'index' is too small to hold all _count FamilyTypeface elements in the collection. It mirrors standard ICollection.CopyTo contract enforcement, throwing before Array.Copy runs so the destination array is not partially modified.
Solutions
- Size the destination array to collection.Count (e.g. new FamilyTypeface[ collection.Count ]) and pass index 0.
- When using an offset, ensure array.Length >= collection.Count + index before calling CopyTo.
- Or use LINQ ToArray()/ToList() instead of manual CopyTo.
Example fix
// before var arr = new FamilyTypeface[3]; collection.CopyTo(arr, 0); // after var arr = new FamilyTypeface[collection.Count]; collection.CopyTo(arr, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (array == null) throw new ArgumentNullException(nameof(array));
if (array.Rank != 1) throw new ArgumentException("array must be 1-D");
if (index >= array.Length) throw new ArgumentOutOfRangeException(nameof(index));
if (collection.Count > array.Length - index)
throw new ArgumentException("array too small at index"); Try / catch
try { collection.CopyTo(array, index); }
catch (ArgumentException ex) { /* resize buffer and retry */ } Prevention
- Always allocate the destination array with collection.Count elements.
- Never reuse buffers sized from a previous snapshot without re-checking Count.
- Prefer ToArray()/ToList() over manual CopyTo.
When it happens
Trigger: Calling FamilyTypefaceCollection.CopyTo(array, index) where _count > array.Length - index, i.e. array has fewer than count+index elements (e.g. copying 5 typefaces into a 3-element array, or into a 10-element array at index 8).
Common situations: Sizing the target array from a stale or wrong count, reusing a buffer sized for a different collection, or passing an offset that leaves insufficient room.
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/7cffda21d37bd77e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs:379
return familyTypeface;
}
private void CopyItems(Array array, int index)
{
ArgumentNullException.ThrowIfNull(array);
if (array.Rank != 1)
throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);
Type elementType = array.GetType().GetElementType();
if (!elementType.IsAssignableFrom(typeof(FamilyTypeface)))
throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(FamilyTypeface[]), elementType));
if (index >= array.Length)
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array"));
if (_count > array.Length - index)
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, "array"));
if (_count != 0)
{
InitializeItemsFromInnerList();
Array.Copy(_items, 0, array, index, _count);
}
}
private class Enumerator : IEnumerator<FamilyTypeface>, SC.IEnumerator
{
private FamilyTypefaceCollection _list;
private int _index;
private FamilyTypeface _current;
internal Enumerator(FamilyTypefaceCollection list)
{
_list = list;
_index = -1;View on GitHub (pinned to 81131a70a4)