{"record":{"id":"a800c4fc064d4ecc","repo":"dotnet/wpf","slug":"sr-gridcollection-destarrayinvalidrank-rowdefinition","errorCode":null,"errorMessage":"SR.GridCollection_DestArrayInvalidRank","messagePattern":"SR\\.GridCollection_DestArrayInvalidRank","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs","lineNumber":73,"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":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs#L55-L91","documentation":"RowDefinition's explicit ICollection.CopyTo implementation validates the destination array before copying. A multi-dimensional (Rank != 1) array cannot be used as the copy destination, so it throws ArgumentException(SR.GridCollection_DestArrayInvalidRank).","triggerScenarios":"Calling CopyTo (or collection initializers / APIs that use it) on a RowDefinitionCollection with a 2D array: ((ICollection)grid.RowDefinitions).CopyTo(twoDimArray, 0).","commonSituations":"Reusing a Rect[,] or object[,] scratch array for the copy; generic serialization code that allocates multidimensional arrays; interop code assuming any Array works.","solutions":["Pass a single-dimensional array, e.g. new RowDefinition[grid.RowDefinitions.Count].","Cast multidimensional data to a flat 1D array first.","Add a Rank == 1 check in calling code to fail early with a clearer message."],"exampleFix":"// before\nvar dest = new RowDefinition[rows, 1];\n((ICollection)grid.RowDefinitions).CopyTo(dest, 0);\n// after\nvar dest = new RowDefinition[grid.RowDefinitions.Count];\n((ICollection)grid.RowDefinitions).CopyTo(dest, 0);","handlingStrategy":"type-guard","validationCode":"if (dest == null) throw new ArgumentNullException(nameof(dest));\nif (dest.Rank != 1) dest = new RowDefinition[grid.RowDefinitions.Count];\n((ICollection)grid.RowDefinitions).CopyTo(dest, 0);","typeGuard":"bool IsSingleDimensional(Array a) => a.Rank == 1;","tryCatchPattern":"try { ((ICollection)grid.RowDefinitions).CopyTo(dest, 0); }\ncatch (ArgumentException) { dest = new RowDefinition[grid.RowDefinitions.Count]; ((ICollection)grid.RowDefinitions).CopyTo(dest, 0); }","preventionTips":["Always allocate one-dimensional arrays for ICollection.CopyTo destinations.","Avoid generic Array-typed scratch buffers that may be multidimensional.","Use the strongly-typed CopyTo/ToArray style helpers when available."],"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-22T01:17:13.364Z"}