dotnet/wpf · error · ArgumentException

throw new ArgumentException(SR.CopyToNotEnoughSpace…

Error message

throw new ArgumentException(SR.CopyToNotEnoughSpace, nameof(index));

What it means

IndexedEnumerable.CopyTo(array, index) enumerates the source and writes into the destination array. If the source has more elements than there is room for from 'index' to the end of the array, the copy cannot complete and ArgumentException (paramName 'index', message CopyToNotEnoughSpace) is thrown.

Solutions

  1. Size the destination array to source Count + index (array = new T[index + count]).
  2. Re-read Count immediately before allocating, on the same thread that mutates the collection.
  3. Pass arrayIndex 0 when copying the whole collection into a fresh array.
  4. Wrap the copy in a lock/measure-then-copy pattern if the source can change concurrently.

Example fix

// before
var arr = new object[count];
enumerable.CopyTo(arr, 5); // needs count+5 slots
// after
var arr = new object[count + 5];
enumerable.CopyTo(arr, 5);
Defensive patterns

Strategy: validation

Validate before calling

int needed = index + count;
if (array.Length - index < count)
    array = new object[needed];

Try / catch

try { e.CopyTo(arr, index); }
catch (ArgumentException ex) when (ex.ParamName == "index") { arr = new object[index + e.Count]; e.CopyTo(arr, index); }

Prevention

When it happens

Trigger: Calling CopyTo with an array whose length minus 'index' is smaller than the number of items in the underlying enumerable — e.g. an array sized to Count before the collection grew, or a nonzero start index on a tight array.

Common situations: Caching collection contents into a preallocated array while the collection is concurrently appended to; copying with a nonzero arrayIndex without accounting for remaining capacity.

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/6729e75fb247e0eb. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/IndexedEnumerable.cs:320

            {
                ic.CopyTo(array, index);
            }
            else
            {
                IList list = (IList)array;

                foreach (object item in collection)
                {
                    if (index < array.Length)
                    {
                        list[index] = item;
                        ++index;
                    }
                    else
                    {
                        // The number of elements in the source ICollection is greater than
                        // the available space from index to the end of the destination array.
                        throw new ArgumentException(SR.CopyToNotEnoughSpace, nameof(index));
                    }
                }
            }
        }


        internal void Invalidate()
        {
            ClearAllCaches();

            // only track changes if source collection isn't already of type IList
            if (List == null)
            {
                INotifyCollectionChanged icc = Enumerable as INotifyCollectionChanged;
                if (icc != null)
                {
                    CollectionChangedEventManager.RemoveHandler(icc, OnCollectionChanged);
                }

View on GitHub (pinned to 81131a70a4)