dotnet/wpf · error · ArgumentException

SR.TableCollectionRangeOutOfRange

Error message

SR.TableCollectionRangeOutOfRange

What it means

TableColumnCollectionInternal.RemoveRange throws this ArgumentException when the requested range [index, index+count) extends past the end of the collection (Size - index < count). The library refuses to partially remove beyond existing items, since a range removal must be fully in bounds.

Solutions

  1. Clamp count to the available items: count = Math.Min(count, collection.Count - index).
  2. Re-read collection.Count immediately before the call instead of caching it.
  3. Guard with if (index + count <= collection.Count) before calling.
  4. Loop calling RemoveAt(index) count times if range semantics must tolerate truncation.

Example fix

// before
collection.RemoveRange(index, count); // may exceed collection size
// after
count = Math.Min(count, collection.Count - index);
if (count > 0) collection.RemoveRange(index, count);
Defensive patterns

Strategy: validation

Validate before calling

count = Math.Min(count, collection.Count - index);
if (index >= 0 && count > 0)
    collection.RemoveRange(index, count);

Type guard

static bool RangeInBounds(ICollection c, int index, int count) => index >= 0 && index < c.Count && count >= 0 && index + count <= c.Count;

Try / catch

try { collection.RemoveRange(index, count); }
catch (ArgumentException ex) { /* range exceeded Size; clamp and retry once */ }

Prevention

When it happens

Trigger: RemoveRange(index, count) where index is valid but index + count > Size, e.g. RemoveRange(3, 10) on a 5-column table, or a stale index/count pair taken before other items were removed.

Common situations: Removing a recorded range after the table was edited elsewhere, off-by-one count calculations, or using Count from a different collection instance.

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/225a960adf668284. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableColumnCollectionInternal.cs:279

        /// The TItems that follow the removed TItems move up to occupy
        /// the vacated spot. The indices of the TItems that are moved are
        /// also updated.
        /// </remarks>
        public override void RemoveRange(int index, int count)
        {
            Version++;

            if (index < 0 || index >= Size)
            {
                throw new ArgumentOutOfRangeException(SR.TableCollectionOutOfRange);
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(SR.TableCollectionCountNeedNonNegNum);
            }
            if (Size - index < count)
            {
                throw new ArgumentException(SR.TableCollectionRangeOutOfRange);
            }

            if (count > 0)
            {
                for (int i = index + count - 1; i >= index; --i)
                {
                    Debug.Assert(BelongsToOwner(Items[i]));

                    PrivateDisconnectChild(Items[i]);
                }

                Size -= count;
                for (int i = index; i < Size; ++i)
                {
                    Debug.Assert(BelongsToOwner(Items[i + count]));

                    Items[i] = Items[i + count];
                    Items[i].Index = i;

View on GitHub (pinned to 81131a70a4)