dotnet/wpf · error

SR.TableCollectionInvalidOffLen

Error message

SR.TableCollectionInvalidOffLen

What it means

ContentElementCollection.CopyTo validates that the destination array has enough room: array.Length - index must be at least Size (the collection count). When available space is insufficient it throws ArgumentException with TableCollectionInvalidOffLen. This prevents Array.Copy from failing partially or unexpectedly and enforces the ICollection.CopyTo contract.

Solutions

  1. Re-read collection.Count and allocate the array immediately before CopyTo
  2. Verify array.Length - index >= collection.Count before calling, throwing a clear error or resizing
  3. Use ToArray()/ToList() to let the framework allocate correctly sized storage

Example fix

// before
var arr = new ContentElement[2];
collection.CopyTo(arr, 0);
// after
var arr = new ContentElement[collection.Count];
collection.CopyTo(arr, 0);
Defensive patterns

Strategy: validation

Validate before calling

int required = collection.Count + index;
if (array.Length < required) array = new ContentElement[required];
collection.CopyTo(array, index);

Try / catch

try { collection.CopyTo(array, index); }
catch (ArgumentException ex) when (ex.Message.Contains("OffLen"))
{
    var sized = new ContentElement[collection.Count];
    collection.CopyTo(sized, 0);
}

Prevention

When it happens

Trigger: Calling CopyTo with an array whose Length minus index is smaller than the collection count, e.g. collection.CopyTo(new ContentElement[2], 0) on a 5-element collection, or CopyTo(array, array.Length - 1) leaving only one slot.

Common situations: Sizing the array from a stale Count captured before the collection changed; reusing a cached buffer across collections of different sizes; copying at an offset into a right-sized array.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

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

        /// <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>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <see cref="ICollection.CopyTo"/>
        /// </exception>
        /// <param name="array"><see cref="ICollection.CopyTo"/></param>

View on GitHub (pinned to 81131a70a4)