{"record":{"id":"f27c44e85a4ab454","repo":"dotnet/wpf","slug":"sr-gridcollection-destarrayinvalidrank","errorCode":null,"errorMessage":"SR.GridCollection_DestArrayInvalidRank","messagePattern":"SR\\.GridCollection_DestArrayInvalidRank","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs","lineNumber":78,"sourceCode":"        #endregion Constructors\n\n        //------------------------------------------------------\n        //\n        //  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>","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs#L60-L96","documentation":"The explicit ICollection.CopyTo implementation on ColumnDefinitionCollection requires the destination Array to be single-dimensional (Rank == 1). Passing a multidimensional array (Rank > 1) throws ArgumentException with SR.GridCollection_DestArrayInvalidRank, because the collection can only copy into a flat array.","triggerScenarios":"Calling ((ICollection)grid.ColumnDefinitions).CopyTo(multiDimArray, 0) where multiDimArray was created with ranks > 1 (e.g. new object[2,2]).","commonSituations":"Copying grid definitions into a shared 2D scratch array; generic serialization code that allocates multidimensional buffers and passes them to ICollection.CopyTo.","solutions":["Pass a one-dimensional array, e.g. new ColumnDefinition[grid.ColumnDefinitions.Count] or object[...].","Copy element-by-element into the multidimensional array with a for loop over the indices.","Use LINQ: grid.ColumnDefinitions.Cast<object>().ToArray() to get a flat array."],"exampleFix":"// before\nvar arr = new object[defs.Count, 2];\n((ICollection)defs).CopyTo(arr, 0);\n// after\nvar arr = new object[defs.Count];\n((ICollection)defs).CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (array.Rank != 1) throw new ArgumentException(\"Destination array must be 1-D\", nameof(array));","typeGuard":"static bool IsFlatArray(Array a) => a != null && a.Rank == 1;","tryCatchPattern":"try { ((ICollection)defs).CopyTo(array, 0); }\ncatch (ArgumentException ex) { log.Error(\"CopyTo requires a single-dimensional array\", ex); }","preventionTips":["Always pass 1-D arrays to ICollection.CopyTo","Prefer the strongly-typed CopyTo(T[], int) over the non-generic one","Watch for C# implicit conversion of multidimensional arrays to Array"],"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-22T01:17:13.364Z"}