{"record":{"id":"d7afd0552b3de5d0","repo":"dotnet/wpf","slug":"sr-tablecollectioncountneednonnegnum","errorCode":null,"errorMessage":"SR.TableCollectionCountNeedNonNegNum","messagePattern":"SR\\.TableCollectionCountNeedNonNegNum","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableColumnCollectionInternal.cs","lineNumber":275,"sourceCode":"        /// <exception cref=\"ArgumentException\">\n        /// <c>index</c> and <c>count</c> do not denote a valid range of TItems in the ContentElementCollection.\n        /// </exception>\n        /// <remarks>\n        /// 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                {","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableColumnCollectionInternal.cs#L257-L293","documentation":"TableColumnCollectionInternal.RemoveRange validates its (index, count) arguments before removing a range of table columns. This ArgumentOutOfRangeException is thrown when the requested count is negative. The library rejects it up front because a negative count is never a valid removal request.","triggerScenarios":"Calling RemoveRange on a TableColumnCollectionInternal (or a TableColumnCollection that routes to it) with a count computed from a subtraction or variable that evaluates to less than 0, e.g. RemoveRange(0, -1) or RemoveRange(2, itemsAfter - itemsBefore) when the delta is negative.","commonSituations":"Developers computing the count from bounds that were already updated, off-by-one subtractions (newLast - oldLast), or passing a default/uninitialized int that remained negative.","solutions":["Compute the count before the call and clamp with Math.Max(0, count) or skip the call when count <= 0.","Check the arithmetic that produces the count; ensure the source bounds are captured before mutation.","If the range should extend to the end, pass the correct remaining size (Size - index) instead of a sentinel negative."],"exampleFix":"// before\ncollection.RemoveRange(start, end - start); // throws if end < start\n// after\nif (end > start)\n{\n    collection.RemoveRange(start, end - start);\n}","handlingStrategy":"validation","validationCode":"if (count < 0)\n    throw new ArgumentOutOfRangeException(nameof(count));\nif (count > 0)\n    collection.RemoveRange(index, count);","typeGuard":"static bool IsValidRemoveRange(ICollection c, int index, int count) => index >= 0 && index < c.Count && count >= 0 && count <= c.Count - index;","tryCatchPattern":"try { collection.RemoveRange(index, count); }\ncatch (ArgumentOutOfRangeException ex) { /* ex.ParamName == \"count\"; log and skip */ }","preventionTips":["Never pass a computed difference without Math.Max(0, ...)","Skip RemoveRange calls when count == 0","Capture range bounds before any mutation of the collection"],"tags":["wpf","argument-out-of-range","table","collection"],"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"}