dotnet/wpf · error

SR.TableCollectionOutOfRangeNeedNonNegNum

Error message

SR.TableCollectionOutOfRangeNeedNonNegNum

What it means

ContentElementCollection.CopyTo throws ArgumentOutOfRangeException when the destination index is negative. Non-negative start index is a requirement of the ICollection.CopyTo contract implemented here; the check runs after array null/rank validation and before the capacity check.

Solutions

  1. Clamp or validate the index with Math.Max(0, index) before calling CopyTo
  2. Ensure the source of the index (e.g. IndexOf result) is checked for -1 before reuse
  3. Use index 0 when copying to the start of a fresh array

Example fix

// before
int idx = list.IndexOf(item); // may be -1
collection.CopyTo(array, idx);
// after
int idx = list.IndexOf(item);
if (idx >= 0) collection.CopyTo(array, idx);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), index, "Index must be non-negative");
collection.CopyTo(array, index);

Try / catch

try { collection.CopyTo(array, index); }
catch (ArgumentOutOfRangeException ex)
{
    logger.LogWarning("CopyTo rejected negative index {Index}", ex.ActualValue);
    collection.CopyTo(array, Math.Max(0, index));
}

Prevention

When it happens

Trigger: Calling CopyTo with index < 0, e.g. collection.CopyTo(array, -1), or an index computed from an expression that evaluated negative (bad offset arithmetic, default-initialized variable, off-by-one decrement).

Common situations: Computing the insert position from a found index of -1 (e.g. IndexOf miss used directly as the copy index); copying into a shared buffer with a rolling offset that underflows.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs:73

        /// </exception>
        /// <exception cref="ArgumentException">
        /// <see cref="ICollection.CopyTo"/>
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <see cref="ICollection.CopyTo"/>
        /// </exception>
        /// <param name="array"><see cref="ICollection.CopyTo"/></param>
        /// <param name="index"><see cref="ICollection.CopyTo"/></param>
        public void CopyTo(Array array, int index)
        {
            ArgumentNullException.ThrowIfNull(array);
            if (array.Rank != 1)
            {
                throw new ArgumentException(SR.TableCollectionRankMultiDimNotSupported);
            }
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(index), SR.TableCollectionOutOfRangeNeedNonNegNum);
            }
            if (array.Length - index < Size)
            {
                throw new ArgumentException(SR.TableCollectionInvalidOffLen);
            }

            Array.Copy(Items, 0, array, index, Size);
        }

        /// <summary>
        /// Strongly typed version of ICollection.CopyTo.
        /// </summary>
        /// <exception cref="ArgumentNullException">
        /// <see cref="ICollection.CopyTo"/>
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <see cref="ICollection.CopyTo"/>
        /// </exception>

View on GitHub (pinned to 81131a70a4)