{"record":{"id":"5ed9a5d53e991878","repo":"dotnet/wpf","slug":"sr-tablecollectionrangeoutofrange-rowdefinition","errorCode":null,"errorMessage":"SR.TableCollectionRangeOutOfRange","messagePattern":"SR\\.TableCollectionRangeOutOfRange","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs","lineNumber":308,"sourceCode":"        ///     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\n                _size -= count;\n                for (int i = index; i < _size; ++i)\n                {\n                    Debug.Assert(   _items[i + count] != null","sourceCodeStart":290,"sourceCodeEnd":326,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs#L290-L326","documentation":"Thrown by RowDefinitionCollection.RemoveRange when the requested range extends past the end of the collection: _size - index < count means fewer than 'count' elements exist from 'index' onward. ArgumentException with SR.TableCollectionRangeOutOfRange requires the caller to request an in-bounds range.","triggerScenarios":"Calling grid.RowDefinitions.RemoveRange(index, count) where index + count > grid.RowDefinitions.Count, e.g. RemoveRange(2, 5) on a 4-row grid.","commonSituations":"Batch-removing rows sized from a stale Count, arithmetic off-by-one in a resize routine, or removing a computed span that assumed the collection was larger.","solutions":["Clamp count: count = Math.Min(count, rowDefs.Count - index) after validating index","Check index + count <= rowDefs.Count before calling","Re-read Count immediately before computing the range"],"exampleFix":"// before\ngrid.RowDefinitions.RemoveRange(start, removeCount);\n// after\nremoveCount = Math.Min(removeCount, grid.RowDefinitions.Count - start);\nif (removeCount > 0) grid.RowDefinitions.RemoveRange(start, removeCount);","handlingStrategy":"validation","validationCode":"int safeCount = Math.Min(count, grid.RowDefinitions.Count - index);\nif (index >= 0 && index < grid.RowDefinitions.Count && safeCount > 0)\n    grid.RowDefinitions.RemoveRange(index, safeCount);","typeGuard":null,"tryCatchPattern":"try { grid.RowDefinitions.RemoveRange(index, count); }\ncatch (ArgumentException) { /* range exceeded Count; clamp and retry */ }","preventionTips":["Clamp count to Count - index before every RemoveRange","Refresh Count after any intervening Add/Clear","Prefer Clear() when the range spans the whole collection"],"tags":["wpf","argument-out-of-range","collection"],"backgroundTag":"index-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"}