{"record":{"id":"b302c843a3a30e44","repo":"dotnet/wpf","slug":"sr-gridcollection-destarrayinvalidlength","errorCode":null,"errorMessage":"SR.GridCollection_DestArrayInvalidLength","messagePattern":"SR\\.GridCollection_DestArrayInvalidLength","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs","lineNumber":81,"sourceCode":"        #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        {\n            ArgumentNullException.ThrowIfNull(array);\n            if (index < 0)\n            {\n                throw new ArgumentOutOfRangeException(SR.Format(SR.GridCollection_DestArrayInvalidLowerBound, \"index\"));","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs#L63-L99","documentation":"RowDefinition's ICollection.CopyTo verifies that the destination array has room: if array.Length - index is less than the number of items (_size), the copy would overrun, so it throws ArgumentException(SR.Format(SR.GridCollection_DestArrayInvalidLength, \"array\")).","triggerScenarios":"Calling CopyTo with an array smaller than grid.RowDefinitions.Count, or with an index that leaves insufficient remaining space (e.g. array length 5, index 3, collection size 4).","commonSituations":"Caching a destination array from a previous larger collection and reusing it after rows were added; allocating exactly Count elements but starting at a nonzero index; off-by-one in size computation.","solutions":["Allocate the array as collection.Count - index elements or larger before calling CopyTo.","Reuse an array only if its length still fits: check array.Length - index >= collection.Count.","Copy into a freshly sized array each time instead of caching buffers."],"exampleFix":"// before\nvar dest = new RowDefinition[3]; // rows grew to 5\n((ICollection)grid.RowDefinitions).CopyTo(dest, 0);\n// after\nvar dest = new RowDefinition[grid.RowDefinitions.Count];\n((ICollection)grid.RowDefinitions).CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"if (dest == null || dest.Length - index < grid.RowDefinitions.Count)\n    dest = new RowDefinition[grid.RowDefinitions.Count];\n((ICollection)grid.RowDefinitions).CopyTo(dest, index);","typeGuard":"bool HasRoomForCopy(Array a, int index, int count) => a != null && a.Length - index >= count;","tryCatchPattern":"try { ((ICollection)grid.RowDefinitions).CopyTo(dest, index); }\ncatch (ArgumentException) { dest = new RowDefinition[grid.RowDefinitions.Count]; ((ICollection)grid.RowDefinitions).CopyTo(dest, 0); }","preventionTips":["Size the destination as collection.Count - index before every CopyTo call.","Re-check cached buffer lengths whenever the collection may have grown.","Prefer LINQ Cast<T>().ToArray() to avoid manual buffer management."],"tags":["wpf","grid","collection","argument-exception"],"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"}