dotnet/wpf · error · ArgumentException

SR.GridCollection_DestArrayInvalidLength

Error message

SR.GridCollection_DestArrayInvalidLength

What it means

RowDefinition's ICollection.CopyTo verifies that the destination array has room: if array.Length - index is less than the number of items (_size), the copy would overrun, so it throws ArgumentException(SR.Format(SR.GridCollection_DestArrayInvalidLength, "array")).

Solutions

  1. Allocate the array as collection.Count - index elements or larger before calling CopyTo.
  2. Reuse an array only if its length still fits: check array.Length - index >= collection.Count.
  3. Copy into a freshly sized array each time instead of caching buffers.

Example fix

// before
var dest = new RowDefinition[3]; // rows grew to 5
((ICollection)grid.RowDefinitions).CopyTo(dest, 0);
// after
var dest = new RowDefinition[grid.RowDefinitions.Count];
((ICollection)grid.RowDefinitions).CopyTo(dest, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (dest == null || dest.Length - index < grid.RowDefinitions.Count)
    dest = new RowDefinition[grid.RowDefinitions.Count];
((ICollection)grid.RowDefinitions).CopyTo(dest, index);

Type guard

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

Try / catch

try { ((ICollection)grid.RowDefinitions).CopyTo(dest, index); }
catch (ArgumentException) { dest = new RowDefinition[grid.RowDefinitions.Count]; ((ICollection)grid.RowDefinitions).CopyTo(dest, 0); }

Prevention

When it happens

Trigger: Calling CopyTo with an array smaller than grid.RowDefinitions.Count, or with an index that leaves insufficient remaining space (e.g. array length 5, index 3, collection size 4).

Common situations: Caching a destination array from a previous larger collection and reusing it after rows were added; allocating exactly Count elements but starting at a nonzero index; off-by-one in size computation.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs:81

        #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(RowDefinition[] 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)