{"record":{"id":"9e0720b5605fbaed","repo":"dotnet/wpf","slug":"sr-format-sr-gridcollection-destarrayinvalidlength-array","errorCode":null,"errorMessage":"SR.Format(SR.GridCollection_DestArrayInvalidLength, \"array\")","messagePattern":"SR\\.Format\\(SR\\.GridCollection_DestArrayInvalidLength, \"array\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs","lineNumber":86,"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(ColumnDefinition[] 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":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs#L68-L104","documentation":"ICollection.CopyTo verifies that the destination array has enough room: array.Length - index must be at least the collection count (_size). If not, it throws ArgumentException with SR.GridCollection_DestArrayInvalidLength (parameter \"array\"), protecting against partial copies and Array.Copy failures.","triggerScenarios":"Calling ((ICollection)grid.ColumnDefinitions).CopyTo(smallArray, index) where smallArray.Length - index < grid.ColumnDefinitions.Count.","commonSituations":"Reusing a cached array after the grid gained more ColumnDefinitions; copying at a non-zero offset into an array sized exactly for the collection count.","solutions":["Size the destination as defs.Count + index before copying.","Re-allocate the array each time instead of reusing a cached buffer.","Use ToArray()/LINQ to let the framework size the array."],"exampleFix":"// before\nvar arr = new object[defs.Count];\n((ICollection)defs).CopyTo(arr, 5);\n// after\nvar arr = new object[defs.Count + 5];\n((ICollection)defs).CopyTo(arr, 5);","handlingStrategy":"validation","validationCode":"if (array.Length - index < defs.Count)\n    throw new ArgumentException(\"Destination array too small\", nameof(array));","typeGuard":"static bool ArrayHasRoom(Array a, int index, int count) => a != null && a.Length - index >= count;","tryCatchPattern":"try { ((ICollection)defs).CopyTo(array, index); }\ncatch (ArgumentException) { array = new object[defs.Count + index]; ((ICollection)defs).CopyTo(array, index); }","preventionTips":["Size arrays as collection.Count + index","Re-check Count right before copying; collections can grow","Prefer ToArray()/LINQ over manual buffer reuse"],"tags":["wpf","grid","array","argument"],"backgroundTag":"invalid-argument-value","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"}