dotnet/wpf · error · ArgumentException

SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength

Error message

SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength

What it means

PartialArray<T>.CopyTo throws ArgumentException wrapping Collection_CopyTo_NumberOfElementsExceedsArrayLength when the destination array has insufficient room: (array.Length - Count - arrayIndex) < 0, meaning Count elements starting at arrayIndex do not fit.

Solutions

  1. Allocate the destination with length >= arrayIndex + partialArray.Count.
  2. Check (array.Length - arrayIndex) >= Count before calling CopyTo.
  3. Copy only the portion that fits by adjusting ranges manually if a partial copy is intended.

Example fix

// before
partialArray.CopyTo(smallBuffer, offset);
// after
if (smallBuffer.Length - offset >= partialArray.Count)
    partialArray.CopyTo(smallBuffer, offset);
Defensive patterns

Strategy: validation

Validate before calling

if (array.Length - arrayIndex < coll.Count) array = new T[arrayIndex + coll.Count];

Type guard

bool HasRoomForCopy(Array dest, int arrayIndex, int count) => dest.Length - arrayIndex >= count;

Try / catch

try { coll.CopyTo(dest, arrayIndex); }
catch (ArgumentException) { dest = new T[arrayIndex + coll.Count]; coll.CopyTo(dest, arrayIndex); }

Prevention

When it happens

Trigger: Calling CopyTo(array, arrayIndex) where array.Length < Count + arrayIndex — e.g. copying a full PartialArray into a smaller buffer or at a nonzero offset.

Common situations: Paging/batching code that reuses undersized scratch buffers, or assuming Count is smaller than it actually is.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/3ade6f7cf53f2731. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs:147

                    SR.Collection_CopyTo_ArrayCannotBeMultidimensional, 
                    nameof(array));                
            }

            ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);

            if (arrayIndex >= array.Length)
            {
                throw new ArgumentException(
                    SR.Format(
                        SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, 
                        "arrayIndex", 
                        "array"),
                        nameof(arrayIndex));
            }

            if ((array.Length - Count - arrayIndex) < 0)
            {
                throw new ArgumentException(
                    SR.Format(
                        SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength,
                        "arrayIndex",
                        "array"));
            }           
            

            // do the copying here
            for (int i = 0; i < Count; i++)
            {
                array[arrayIndex + i] = this[i];
            }
        }

        #endregion

        #region IEnumerable<T> Members

View on GitHub (pinned to 81131a70a4)