dotnet/wpf · error · ArgumentOutOfRangeException

SR.GridCollection_DestArrayInvalidLowerBound

Error message

SR.GridCollection_DestArrayInvalidLowerBound

What it means

RowDefinition's ICollection.CopyTo requires a non-negative starting index; a negative index has no valid position in the destination array, so it throws ArgumentOutOfRangeException(SR.Format(SR.GridCollection_DestArrayInvalidLowerBound, "index")).

Solutions

  1. Pass an index >= 0; use index 0 to copy from the start.
  2. Compute the index defensively: int idx = Math.Max(0, computedIndex).
  3. Treat 'append' explicitly by using a larger array and index = existingCount, never -1.

Example fix

// before
((ICollection)grid.RowDefinitions).CopyTo(dest, count - 1); // count==0 → -1
// after
int idx = Math.Max(0, count - 1);
((ICollection)grid.RowDefinitions).CopyTo(dest, idx);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0) index = 0;
((ICollection)grid.RowDefinitions).CopyTo(dest, index);

Type guard

bool IsValidCopyIndex(int index) => index >= 0;

Try / catch

try { ((ICollection)grid.RowDefinitions).CopyTo(dest, index); }
catch (ArgumentOutOfRangeException) { ((ICollection)grid.RowDefinitions).CopyTo(dest, 0); }

Prevention

When it happens

Trigger: Calling ((ICollection)grid.RowDefinitions).CopyTo(array, negativeIndex) with index computed as size-1 on an empty collection, or an offset variable that went below zero.

Common situations: index = items.Count - 1 when the collection is empty; loop-computed offsets that underflow; passing -1 as a sentinel for 'append'.

Related errors


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

Appendix: source

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

        //  Public Methods
        //
        //------------------------------------------------------

        #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)
        {

View on GitHub (pinned to 81131a70a4)