{"record":{"id":"f0d84b1a5e185471","repo":"dotnet/wpf","slug":"sr-tablecollectionrangeoutofrange-f0d84b","errorCode":null,"errorMessage":"SR.TableCollectionRangeOutOfRange","messagePattern":"SR\\.TableCollectionRangeOutOfRange","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableTextElementCollectionInternal.cs","lineNumber":229,"sourceCode":"        /// The TItems that follow the removed TItems move up to occupy\n        /// the vacated spot. The indices of the TItems that are moved are\n        /// also updated.\n        /// </remarks>\n        public override void RemoveRange(int index, int count)\n        {\n            Version++;\n\n            if (index < 0 || index >= Size)\n            {\n                throw new ArgumentOutOfRangeException(SR.TableCollectionOutOfRange);\n            }\n            if (count < 0)\n            {\n                throw new ArgumentOutOfRangeException(SR.TableCollectionCountNeedNonNegNum);\n            }\n            if (Size - index < count)\n            {\n                throw new ArgumentException(SR.TableCollectionRangeOutOfRange);\n            }\n\n            if (count > 0)\n            {\n                for (int i = index + count - 1; i >= index; --i)\n                {\n                    Debug.Assert(BelongsToOwner(Items[i]));\n                    Remove(Items[i]);\n                }\n            }\n}\n\n        /// <summary>\n        /// Sets the specified TItem at the specified index;\n        /// Connects the item to the model tree;\n        /// Notifies the TItem about the event.\n        /// </summary>\n        /// <exception cref=\"ArgumentException\">","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableTextElementCollectionInternal.cs#L211-L247","documentation":"RemoveRange validates that the requested range [index, index+count) lies inside the collection. If count is negative it throws ArgumentOutOfRangeException(TableCollectionCountNeedNonNegNum), and if Size - index < count the range extends past the end of the collection and it throws ArgumentException(TableCollectionRangeOutOfRange).","triggerScenarios":"Calling TableTextElementCollectionInternal.RemoveRange(index, count) where index+count exceeds Size, e.g. computing count from stale indices or after items were already removed.","commonSituations":"Batch-removing TableRow/TableCell/Run elements while holding indexes captured before prior mutations; iterating and removing with an index that drifts as the collection shrinks.","solutions":["Recompute index and count immediately before calling RemoveRange against the current collection size.","Clamp: var c = Math.Min(count, collection.Count - index); if (c > 0) collection.RemoveRange(index, c);","Prefer removing items by reference (Remove(item)) or removing from the end of the collection backwards.","Guard with if (index >= 0 && count >= 0 && index + count <= collection.Count) before the call."],"exampleFix":"// before\ncollection.RemoveRange(startIndex, endIndex - startIndex + 1);\n// after\nif (startIndex >= 0 && endIndex < collection.Count)\n    collection.RemoveRange(startIndex, endIndex - startIndex + 1);","handlingStrategy":"validation","validationCode":"if (index < 0 || count < 0 || index + count > collection.Count)\n    throw new ArgumentOutOfRangeException(nameof(index), \"range outside collection\");","typeGuard":"static bool CanRemoveRange<T>(TableTextElementCollectionInternal<T> c, int index, int count)\n    => index >= 0 && count >= 0 && index <= c.Count && count <= c.Count - index;","tryCatchPattern":null,"preventionTips":["Recompute indices immediately before mutation operations.","Remove items by reference when possible.","Iterate collections backwards when removing by index.","Clamp count to the remaining items before calling RemoveRange."],"tags":["wpf","argumentexception","collection","table"],"backgroundTag":"argument-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}