{"record":{"id":"1a548360cc4dae20","repo":"dotnet/wpf","slug":"sr-tablecollectioncountneednonnegnum-rowdefinition","errorCode":null,"errorMessage":"SR.TableCollectionCountNeedNonNegNum","messagePattern":"SR\\.TableCollectionCountNeedNonNegNum","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs","lineNumber":304,"sourceCode":"        /// <summary>\n        ///     Removes a range of RowDefinitions from the RowDefinitionCollection.\n        /// </summary>\n        /// <param name=\"index\">\n        ///     The zero-based index of the range of RowDefinitions to remove.\n        /// </param>\n        /// <param name=\"count\">\n        ///     The number of RowDefinitions to remove.\n        /// </param>\n        public void RemoveRange(int index, int count)\n        {\n            PrivateVerifyWriteAccess();\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            PrivateOnModified();\n\n            if (count > 0)\n            {\n                for (int i = index + count - 1; i >= index; --i)\n                {\n                    Debug.Assert(   _items[i] != null\n                                &&  _items[i].Parent == _owner );\n\n                    PrivateDisconnectChild(_items[i]);\n                }\n","sourceCodeStart":286,"sourceCodeEnd":322,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs#L286-L322","documentation":"Thrown by RowDefinitionCollection.RemoveRange when count is negative. A negative removal count is meaningless, so ArgumentOutOfRangeException with SR.TableCollectionCountNeedNonNegNum is thrown after the index check passes.","triggerScenarios":"Calling grid.RowDefinitions.RemoveRange(index, count) with count < 0, e.g. RemoveRange(0, desired - current) where desired < current, producing a negative delta.","commonSituations":"Computing the count as a difference of row targets without ordering the subtraction, subtracting header/footer offsets incorrectly, or integer arithmetic with sign errors.","solutions":["Compute count as Math.Max(0, targetCount - currentCount)","Check count >= 0 before calling RemoveRange","Use Math.Abs only if a positive count was truly intended"],"exampleFix":"// before\ngrid.RowDefinitions.RemoveRange(start, desiredRows - grid.RowDefinitions.Count);\n// after\nint removeCount = Math.Max(0, grid.RowDefinitions.Count - desiredRows);\ngrid.RowDefinitions.RemoveRange(grid.RowDefinitions.Count - removeCount, removeCount);","handlingStrategy":"validation","validationCode":"if (count < 0) count = 0;\nif (index >= 0 && index < grid.RowDefinitions.Count)\n    grid.RowDefinitions.RemoveRange(index, Math.Min(count, grid.RowDefinitions.Count - index));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When count is a difference, order the subtraction or use Math.Max(0, diff)","Assert counts are non-negative in debug builds","Centralize row-resize logic in one validated helper"],"tags":["wpf","argument-out-of-range","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"}