dotnet/wpf · error · ArgumentException
SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayL…
Error message
SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, "array")
What it means
FamilyMapCollection.CopyTo throws ArgumentException when the collection has more elements than the destination array can hold from the given index onward (_count > array.Length - index). This prevents partial/overflow copies per the ICollection.CopyTo contract.
Solutions
- Allocate the destination array with length >= index + collection.Count (or exactly collection.Count with index 0).
- Re-query collection.Count immediately before CopyTo instead of caching sizes.
- Use ToArray() when a correctly sized buffer is all that is needed.
Example fix
// before var buffer = new FontFamilyMap[4]; familyMaps.CopyTo(buffer, 0); // after var buffer = new FontFamilyMap[familyMaps.Count]; familyMaps.CopyTo(buffer, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (array.Length - index < col.Count) array = new FontFamilyMap[col.Count];
Try / catch
try { col.CopyTo(array, index); }
catch (ArgumentException) { array = new FontFamilyMap[col.Count]; col.CopyTo(array, 0); } Prevention
- Re-query Count right before copying
- Prefer ToArray() over manual buffers
When it happens
Trigger: Calling CopyTo with an array shorter than the collection (offset by index), e.g. an array allocated for an older, smaller collection or a hardcoded size.
Common situations: Caching a FontFamilyMap[] sized once and reusing it after the composite font gains more family maps, or allocating exactly Count but passing a nonzero index.
Related errors
- Collection_CopyTo_NumberOfElementsExceedsArrayLength
- SR.Collection_CopyTo_ArrayCannotBeMultidimensional
- SR.CompositeFont_TooManyFamilyMaps
- SR.Format(SR.CannotConvertType, typeof(FamilyTypeface)…
- SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArra…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/5863ed8dc01da22c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs:80
/// Determines whether the FontFamily contains the specified FontFamilyMap.
/// </summary>
public bool Contains(FontFamilyMap item)
{
return FindItem(item) >= 0;
}
/// <summary>
/// Copies the contents of the list to the specified array.
/// </summary>
public void CopyTo(FontFamilyMap[] array, int index)
{
ArgumentNullException.ThrowIfNull(array);
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)
Array.Copy(_items, 0, array, index, _count);
}
void SC.ICollection.CopyTo(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"));View on GitHub (pinned to 81131a70a4)