dotnet/wpf · error · ArgumentException

SR.Format(SR.GridCollection_DestArrayInvalidLength, "array")

Error message

SR.Format(SR.GridCollection_DestArrayInvalidLength, "array")

What it means

ICollection.CopyTo verifies that the destination array has enough room: array.Length - index must be at least the collection count (_size). If not, it throws ArgumentException with SR.GridCollection_DestArrayInvalidLength (parameter "array"), protecting against partial copies and Array.Copy failures.

Solutions

  1. Size the destination as defs.Count + index before copying.
  2. Re-allocate the array each time instead of reusing a cached buffer.
  3. Use ToArray()/LINQ to let the framework size the array.

Example fix

// before
var arr = new object[defs.Count];
((ICollection)defs).CopyTo(arr, 5);
// after
var arr = new object[defs.Count + 5];
((ICollection)defs).CopyTo(arr, 5);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

static bool ArrayHasRoom(Array a, int index, int count) => a != null && a.Length - index >= count;

Try / catch

try { ((ICollection)defs).CopyTo(array, index); }
catch (ArgumentException) { array = new object[defs.Count + index]; ((ICollection)defs).CopyTo(array, index); }

Prevention

When it happens

Trigger: Calling ((ICollection)grid.ColumnDefinitions).CopyTo(smallArray, index) where smallArray.Length - index < grid.ColumnDefinitions.Count.

Common situations: Reusing a cached array after the grid gained more ColumnDefinitions; copying at a non-zero offset into an array sized exactly for the collection count.

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/9e0720b5605fbaed. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs:86

        #region Public Methods

        /// <summary>
        ///     <see cref="ICollection.CopyTo"/>
        /// </summary>
        void ICollection.CopyTo(Array array, int index)
        {
            ArgumentNullException.ThrowIfNull(array);
            if (array.Rank != 1)
            {
                throw new ArgumentException(SR.GridCollection_DestArrayInvalidRank);
            }
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException(SR.Format(SR.GridCollection_DestArrayInvalidLowerBound, "index"));
            }
            if (array.Length - index < _size)
            {
                throw new ArgumentException(SR.Format(SR.GridCollection_DestArrayInvalidLength, "array"));
            }

            if (_size > 0)
            {
                Debug.Assert(_items != null);
                Array.Copy(_items, 0, array, index, _size);
            }
        }

        /// <summary>
        ///     <see cref="ICollection<T>.CopyTo"/>
        /// </summary>
        public void CopyTo(ColumnDefinition[] array, int index) //  void ICollection<T>.CopyTo(T[] array, int arrayIndex)
        {
            ArgumentNullException.ThrowIfNull(array);
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException(SR.Format(SR.GridCollection_DestArrayInvalidLowerBound, "index"));

View on GitHub (pinned to 81131a70a4)