{"record":{"id":"dc176c751a2d83d2","repo":"dotnet/wpf","slug":"sr-tablecollectioncountneednonnegnum-dc176c","errorCode":null,"errorMessage":"SR.TableCollectionCountNeedNonNegNum","messagePattern":"SR\\.TableCollectionCountNeedNonNegNum","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableTextElementCollectionInternal.cs","lineNumber":225,"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                    Remove(Items[i]);\n                }\n            }\n}\n\n        /// <summary>\n        /// Sets the specified TItem at the specified index;","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableTextElementCollectionInternal.cs#L207-L243","documentation":"TableTextElementCollectionInternal<T>.RemoveRange rejects a negative count with ArgumentOutOfRangeException SR.TableCollectionCountNeedNonNegNum. The check happens after the index check, so index must already be in range; only a negative count triggers this particular error.","triggerScenarios":"RemoveRange(index, count) with count < 0 — typically from a subtraction like end - start where end < start, or an uninitialized/default variable.","commonSituations":"Range bookkeeping where the end was captured before the start was updated, selection-range math on inverted selections, and refactored code that swapped parameter order.","solutions":["Validate/clamp the count first: count = Math.Max(0, count), and skip the call when it is zero.","Fix the subtraction producing the count; compute it as Math.Max(0, end - start).","For inverted ranges, normalize so start <= end before computing the count.","Add a debug assertion on count >= 0 at the call site to catch regressions early."],"exampleFix":"// before\ncollection.RemoveRange(start, end - start); // throws when end < start\n// after\nint count = Math.Max(0, end - start);\nif (count > 0) collection.RemoveRange(start, count);","handlingStrategy":"validation","validationCode":"int count = Math.Max(0, end - start);\nif (count > 0 && start >= 0 && start < collection.Count)\n    collection.RemoveRange(start, count);","typeGuard":"static bool IsValidRange(int start, int end) => end >= start && start >= 0;","tryCatchPattern":"try { collection.RemoveRange(index, count); }\ncount < 0 check: catch (ArgumentOutOfRangeException ex) when (count < 0) { count = 0; }","preventionTips":["Normalize ranges so start <= end before computing count","Clamp every computed count with Math.Max(0, ...)","Assert count >= 0 at call sites in debug builds"],"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-22T01:17:13.364Z"}