{"record":{"id":"7030c4033f5475ad","repo":"dotnet/wpf","slug":"sr-gridcollection-destarrayinvalidlowerbound","errorCode":null,"errorMessage":"SR.GridCollection_DestArrayInvalidLowerBound","messagePattern":"SR\\.GridCollection_DestArrayInvalidLowerBound","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs","lineNumber":77,"sourceCode":"        //  Public Methods\n        //\n        //------------------------------------------------------\n\n        #region Public Methods\n\n        /// <summary>\n        ///     <see cref=\"ICollection.CopyTo\"/>\n        /// </summary>\n        void ICollection.CopyTo(Array array, int index)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n            if (array.Rank != 1)\n            {\n                throw new ArgumentException(SR.GridCollection_DestArrayInvalidRank);\n            }\n            if (index < 0)\n            {\n                throw new ArgumentOutOfRangeException(SR.Format(SR.GridCollection_DestArrayInvalidLowerBound, \"index\"));\n            }\n            if (array.Length - index < _size)\n            {\n                throw new ArgumentException(SR.Format(SR.GridCollection_DestArrayInvalidLength, \"array\"));\n            }\n\n            if (_size > 0)\n            {\n                Debug.Assert(_items != null);\n                Array.Copy(_items, 0, array, index, _size);\n            }\n        }\n\n        /// <summary>\n        ///     <see cref=\"ICollection<T>.CopyTo\"/>\n        /// </summary>\n        public void CopyTo(RowDefinition[] array, int index) //  void ICollection<T>.CopyTo(T[] array, int arrayIndex)\n        {","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs#L59-L95","documentation":"RowDefinition's ICollection.CopyTo requires a non-negative starting index; a negative index has no valid position in the destination array, so it throws ArgumentOutOfRangeException(SR.Format(SR.GridCollection_DestArrayInvalidLowerBound, \"index\")).","triggerScenarios":"Calling ((ICollection)grid.RowDefinitions).CopyTo(array, negativeIndex) with index computed as size-1 on an empty collection, or an offset variable that went below zero.","commonSituations":"index = items.Count - 1 when the collection is empty; loop-computed offsets that underflow; passing -1 as a sentinel for 'append'.","solutions":["Pass an index >= 0; use index 0 to copy from the start.","Compute the index defensively: int idx = Math.Max(0, computedIndex).","Treat 'append' explicitly by using a larger array and index = existingCount, never -1."],"exampleFix":"// before\n((ICollection)grid.RowDefinitions).CopyTo(dest, count - 1); // count==0 → -1\n// after\nint idx = Math.Max(0, count - 1);\n((ICollection)grid.RowDefinitions).CopyTo(dest, idx);","handlingStrategy":"validation","validationCode":"if (index < 0) index = 0;\n((ICollection)grid.RowDefinitions).CopyTo(dest, index);","typeGuard":"bool IsValidCopyIndex(int index) => index >= 0;","tryCatchPattern":"try { ((ICollection)grid.RowDefinitions).CopyTo(dest, index); }\ncatch (ArgumentOutOfRangeException) { ((ICollection)grid.RowDefinitions).CopyTo(dest, 0); }","preventionTips":["Clamp computed indices with Math.Max(0, i).","Never use -1 as an append sentinel with CopyTo.","Check collection Count before computing count-1 style indices."],"tags":["wpf","grid","collection","argument-out-of-range"],"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"}