{"record":{"id":"225a960adf668284","repo":"dotnet/wpf","slug":"sr-tablecollectionrangeoutofrange","errorCode":null,"errorMessage":"SR.TableCollectionRangeOutOfRange","messagePattern":"SR\\.TableCollectionRangeOutOfRange","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableColumnCollectionInternal.cs","lineNumber":279,"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\n                    PrivateDisconnectChild(Items[i]);\n                }\n\n                Size -= count;\n                for (int i = index; i < Size; ++i)\n                {\n                    Debug.Assert(BelongsToOwner(Items[i + count]));\n\n                    Items[i] = Items[i + count];\n                    Items[i].Index = i;","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableColumnCollectionInternal.cs#L261-L297","documentation":"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.","triggerScenarios":"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.","commonSituations":"Removing a recorded range after the table was edited elsewhere, off-by-one count calculations, or using Count from a different collection instance.","solutions":["Clamp count to the available items: count = Math.Min(count, collection.Count - index).","Re-read collection.Count immediately before the call instead of caching it.","Guard with if (index + count <= collection.Count) before calling.","Loop calling RemoveAt(index) count times if range semantics must tolerate truncation."],"exampleFix":"// before\ncollection.RemoveRange(index, count); // may exceed collection size\n// after\ncount = Math.Min(count, collection.Count - index);\nif (count > 0) collection.RemoveRange(index, count);","handlingStrategy":"validation","validationCode":"count = Math.Min(count, collection.Count - index);\nif (index >= 0 && count > 0)\n    collection.RemoveRange(index, count);","typeGuard":"static bool RangeInBounds(ICollection c, int index, int count) => index >= 0 && index < c.Count && count >= 0 && index + count <= c.Count;","tryCatchPattern":"try { collection.RemoveRange(index, count); }\ncatch (ArgumentException ex) { /* range exceeded Size; clamp and retry once */ }","preventionTips":["Always clamp count against Count - index","Re-read Count right before range operations","Prefer Clear() or per-item Remove over risky range math"],"tags":["wpf","argument-exception","table","collection"],"backgroundTag":"value-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}