dotnet/wpf · error · ArgumentException

SR.Arg_ArrayPlusOffTooSmall

Error message

SR.Arg_ArrayPlusOffTooSmall

What it means

CopyTo validates that the destination has room: it throws ArgumentException with SR.Arg_ArrayPlusOffTooSmall when array.Length - index < Count. The array is big enough in total perhaps, but not enough space remains from the given index to hold every element.

Solutions

  1. Size the array to at least index + collection.Count before copying
  2. Copy into a newly allocated array of exact Count size
  3. Check array.Length - index >= collection.Count before calling

Example fix

// before
var target = new WeakReference[collection.Count - 1];
collection.CopyTo(target, 0);
// after
var target = new WeakReference[collection.Count];
collection.CopyTo(target, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array.Length - index < collection.Count) throw new ArgumentException("Destination array too small from the given index.", nameof(array));

Type guard

bool HasRoom(Array a, int index, int count) => a.Length - index >= count;

Try / catch

try { ((ICollection)collection).CopyTo(array, index); } catch (ArgumentException) { array = new T[collection.Count]; ((ICollection)collection).CopyTo(array, 0); }

Prevention

When it happens

Trigger: Calling CopyTo(array, index) where count of elements exceeds array.Length - index, e.g., copying a 5-element collection into an array of length 4, or at index 3 of a length-6 array.

Common situations: Reusing a shared buffer sized for earlier smaller data; off-by-one when reserving space for a terminator element.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs:156

            if (array.Rank != 1) {
                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
                throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);
            }

            if( array.GetLowerBound(0) != 0 ) {
                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
                throw new ArgumentException(SR.Arg_NonZeroLowerBound);
            }

            if (index < 0) {
                //ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
                throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);
            }

            if (array.Length - index < Count) {
                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
                throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall);
            }

            IList<T> dlist = CreateDereferencedList();
            if (array is T[] items)
            {
                dlist.CopyTo(items, index);
            }
            else
            {
                //
                // Catch the obvious case assignment will fail.
                // We can found all possible problems by doing the check though.
                // For example, if the element type of the Array is derived from T,
                // we can't figure out if we can successfully copy the element beforehand.
                //
                Type targetType = array.GetType().GetElementType();
                Type sourceType = typeof(T);
                if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType)))

View on GitHub (pinned to 81131a70a4)