dotnet/wpf · error · ArgumentOutOfRangeException

SR.Format(SR.GridCollection_DestArrayInvalidLowerBound…

Error message

SR.Format(SR.GridCollection_DestArrayInvalidLowerBound, "index")

What it means

The explicit ICollection.CopyTo on ColumnDefinitionCollection requires the index parameter to be non-negative. A negative index throws ArgumentOutOfRangeException with SR.GridCollection_DestArrayInvalidLowerBound (parameter "index").

Solutions

  1. Ensure index >= 0 before calling; clamp or assert on the value.
  2. Fix the offset calculation that produces the negative index.
  3. If copying from scratch, use index 0 into a correctly sized array.

Example fix

// before
int idx = itemsCopied - batch; // can be negative
((ICollection)defs).CopyTo(target, idx);
// after
int idx = Math.Max(0, itemsCopied);
if (idx + defs.Count <= target.Length)
    ((ICollection)defs).CopyTo(target, idx);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0) throw new ArgumentOutOfRangeException(nameof(index));

Type guard

static bool IsValidCopyIndex(int i) => i >= 0;

Try / catch

try { ((ICollection)defs).CopyTo(array, index); }
catch (ArgumentOutOfRangeException) { ((ICollection)defs).CopyTo(array, 0); }

Prevention

When it happens

Trigger: Calling ((ICollection)grid.ColumnDefinitions).CopyTo(array, -1) or with a negative computed offset (e.g. index = cursor where cursor went below zero).

Common situations: Chained copies with a moving offset that underflows; passing an uninitialized/default int or a subtraction result like remaining - copied that turned negative.

Related errors


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

Appendix: source

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

        //  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(ColumnDefinition[] array, int index) //  void ICollection<T>.CopyTo(T[] array, int arrayIndex)
        {

View on GitHub (pinned to 81131a70a4)